鱼C论坛

 找回密码
 立即注册
查看: 1453|回复: 8

[已解决]课后作业30 第四题

[复制链接]
发表于 2021-7-30 18:09:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
def content_find(target):
      all_file = os.listdir(os.curdir())
      list1 =[]
      for each_file in all_file:
            if os.path.splitext(each_file) == 'txt':
                  list1.append(os.getcwd(each_file) + os.sep + os.linesep)
            else:
                  if os.path.isdir(each_file):
                        os.chdir(os.getcwd(each_file))
                        return list1 + content_find(target)
                        os.chdir(os.curdir())


第一步中列出所有含txt的文件路径中,可不可以用递归,可以的话求大佬知道,实在不知道咋写了,不可以的话请大佬指点一下,这种情况下为啥用不来.
最佳答案
2021-7-30 22:51:43
  1. import os


  2. def content_find(target, path, file_list):

  3.     os.chdir(path)
  4.     all_file = os.listdir(os.getcwd())
  5.     for each_file in all_file:
  6.         # 先判断是否为目录
  7.         if os.path.isdir(each_file):
  8.             content_find(target, each_file, file_list)
  9.             os.chdir(os.pardir)

  10.         else:
  11.             # 再判断是否符合要求
  12.             # 分离会有两个值[文件名,拓展名] 用 -1 索引取最后一个元素
  13.             if os.path.splitext(each_file)[-1] == target:
  14.                 # list1.append(os.getcwd(each_file) + os.sep + os.linesep)
  15.                 # 取文件名
  16.                 file_list.append(each_file)


  17. cur_path = os.getcwd()
  18. print("您当前的位置是:", cur_path)
  19. target = input("请问要统计什么类型的文件:[格式:.txt,.py,.c]")
  20. list2 = []
  21. content_find(target, cur_path, list2)
  22. for each_file in list2:
  23.     print(each_file)
复制代码

诶嘿,我改了改你的代码,你看看
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-30 19:28:40 | 显示全部楼层
这是我以前写的,你先看看,我去回顾下
  1. import os
  2. import easygui as eg


  3. def mq(path, code_count, count_type, c_type):
  4.     os.chdir(path)
  5.     for eachfile in os.listdir(os.getcwd()):
  6.         if os.path.isdir(eachfile):
  7.             mq(eachfile, code_count, count_type, c_type)
  8.             os.chdir(os.pardir)
  9.         else:
  10.             (f_name, f_extension) = os.path.splitext(eachfile)
  11.             if f_extension in count_type:
  12.                 count = 0
  13.                 with open(eachfile) as fp:
  14.                     try:
  15.                         for each_line in fp:
  16.                             count += 1
  17.                     except UnicodeDecodeError:
  18.                         pass
  19.                 if f_extension in code_count.keys():
  20.                     code_count[f_extension] += count
  21.                     c_type[f_extension] += 1
  22.                 else:
  23.                     code_count.setdefault(f_extension, count)
  24.                     c_type.setdefault(f_extension, 1)


  25. eg.msgbox('欢迎使用大马强代码统计软件', '\@.@/', ok_button='继续')
  26. Dir = eg.diropenbox(title='请选择要统计的目录')
  27. eg.msgbox('正在统计代码行数...\n请等待10分钟', '提示', ok_button='妈耶!GKD!')

  28. code_count = dict()
  29. c_type = dict()
  30. count_type = ['.py', '.c', '.cpp']
  31. mq(Dir, code_count, count_type, c_type)

  32. total = 0  # 改进函数列表推导式
  33. for each in list(code_count.keys()):
  34.     total += code_count[each]

  35. with open('统计.txt', 'w', encoding='utf-8') as fp:
  36.     for j in list(code_count.keys()):
  37.         fp.write('【%s】源文件%d个,源代码%d\n' % (j, c_type[j], code_count[j]))
  38. with open('统计.txt', encoding='utf-8') as fp:
  39.     msg = '您当前已经积累了%d行代码,完成进度:%.2f%% \n离10w行代码还有%d行,请继续努力!' % (
  40.         total, total/1000, 100000-total)  # 百分数表示【%%】
  41.     eg.textbox(msg, '统计结果', text=fp.read())
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-30 20:21:28 | 显示全部楼层
本帖最后由 大马强 于 2021-7-30 20:30 编辑
  1. import os
  2. def count_txt(cur_path):

  3.     # 改变当前工作位置
  4.     os.chdir(cur_path)
  5.     for each in os.listdir(cur_path):

  6.         if os.path.isdir(each):
  7.             # 递归调用,进入这个目录
  8.             count_txt(each)
  9.             # 目录遍历完毕,返回将工作区改为父目录,返回到上一层
  10.             os.chdir(os.pardir)

  11.         else:
  12.             # 条件匹配
  13.             if os.path.splitext(each)[-1] == ".txt":
  14.                 print(each)


  15. # 当期前位置
  16. cur_path = os.getcwd()
  17. count_txt(cur_path)
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-30 20:30:04 | 显示全部楼层
你这个就是递归了呀
看这里
  1. return list1 + content_find(target)
复制代码

就是递归调用了content_find函数
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-30 21:10:15 | 显示全部楼层
大马强 发表于 2021-7-30 20:30
你这个就是递归了呀
看这里

大佬可以给个联系方式吗?真心想跟您学习.
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-7-30 21:11:04 | 显示全部楼层
大马强 发表于 2021-7-30 20:30
你这个就是递归了呀
看这里

.txt
Traceback (most recent call last):
  File "D:\phython\课后作业30 4.py", line 15, in <module>
    list2 = content_find(target)
  File "D:\phython\课后作业30 4.py", line 10, in content_find
    os.chdir(os.getcwd(each_file))
TypeError: nt.getcwd() takes no arguments (1 given)


出现了这样的错误
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-30 21:18:14 | 显示全部楼层
我也是入门菜鸡,加我qq1432659208
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-30 21:20:16 | 显示全部楼层
  1. s.chdir(os.getcwd(each_file))
复制代码

改成
  1. s.chdir(os.getcwd())
复制代码

试试,我记得应该不带参数的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-30 22:51:43 | 显示全部楼层    本楼为最佳答案   
  1. import os


  2. def content_find(target, path, file_list):

  3.     os.chdir(path)
  4.     all_file = os.listdir(os.getcwd())
  5.     for each_file in all_file:
  6.         # 先判断是否为目录
  7.         if os.path.isdir(each_file):
  8.             content_find(target, each_file, file_list)
  9.             os.chdir(os.pardir)

  10.         else:
  11.             # 再判断是否符合要求
  12.             # 分离会有两个值[文件名,拓展名] 用 -1 索引取最后一个元素
  13.             if os.path.splitext(each_file)[-1] == target:
  14.                 # list1.append(os.getcwd(each_file) + os.sep + os.linesep)
  15.                 # 取文件名
  16.                 file_list.append(each_file)


  17. cur_path = os.getcwd()
  18. print("您当前的位置是:", cur_path)
  19. target = input("请问要统计什么类型的文件:[格式:.txt,.py,.c]")
  20. list2 = []
  21. content_find(target, cur_path, list2)
  22. for each_file in list2:
  23.     print(each_file)
复制代码

诶嘿,我改了改你的代码,你看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-13 01:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表