鱼C论坛

 找回密码
 立即注册
查看: 1832|回复: 6

[已解决]小弟在运用递归的时候出现了问题,请各位大神解惑!!

[复制链接]
发表于 2017-10-2 10:53:19 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 宫保鸡丁丁 于 2017-10-3 19:18 编辑

目的:将文件夹和子文件夹的所有txt文件用字典收集起来.相对路径(也就是文件的位置)作为键,txt文件作为值.键可以对应多个值,实现在当前文件夹搜索多个txt文件

源代码:
  1. def f(path,txt):
  2.     for each in os.listdir(os.curdir):
  3.         (name,extension) = os.path.splitext(each)
  4.         if extension == '.txt':
  5.             if path not in txt:txt[path] = []
  6.             txt[path].append(each)      #将相对路径作为键,txt文件名作为值
  7.         if os.path.isdir(each):
  8.             f(os.path.join(path,each),txt)
  9.             os.chdir(os.pardir)         #返回上目录

  10. path = os.getcwd()
  11. txt = dict()
  12. f(path,txt)
复制代码


错误提示:RecursionError: maximum recursion depth exceeded in comparison

程序如上所示,接下来我的问题是:
        
                 if os.path.isdir(each):
                            f(os.path.join(path,each),txt)
                            os.chdir(os.pardir)         #返回上目录
        我将上面这段删除时,当前文件夹中的txt文件可以收集到.但是进行递归要搜索子文件夹,添加了上面那段程序后却发现有了错误.错误原因是最大递归深度超过了比较,我不明白.

        请求各位大神解解惑

最佳答案
2017-10-3 17:18:45
  1. import os

  2. def f(path, txt):
  3.         for each in os.listdir(path):
  4.                 each = os.path.join(path,each)
  5.                 (name,extension) = os.path.splitext(each)
  6.                 if extension == '.txt':
  7.                         print(each)
  8.                 if os.path.isdir(each):
  9.                         f(os.path.join(path,each), txt)

  10. def ff(path, txt):
  11.         for each in os.listdir(os.curdir):
  12.                 (name,extension) = os.path.splitext(each)
  13.                 if extension == '.txt':
  14.                         print(each)
  15.                 if os.path.isdir(each):
  16.                         os.chdir(os.path.join(os.curdir,each))
  17.                         ff(os.path.join(os.curdir,each), txt)
  18.         os.chdir(os.pardir)
  19.        
  20. path = os.getcwd()
  21. txt = dict()
  22. f(path, txt)
  23. print('============')
  24. ff(path, txt)

复制代码


我想你应该是path 和os.curdir 混合着用了。你用for each in os.listdir(os.curdir)遍历当前目录下的所有文件,但是你调用函数的时候并没有改变当前的目录。这是我修改后的,你可以看看哈~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-10-2 23:15:21 | 显示全部楼层
  1. import os

  2. txt = []
  3. def FileToTxt(dir,txt):
  4.     files = os.listdir(dir)
  5.     print(files)
  6.     for name in files:
  7.         fullname=os.path.join(dir,name)
  8.         if(os.path.isdir(fullname)):
  9.             FileToTxt(fullname,txt)
  10.         else:
  11.             if(name.endswith('txt')):
  12.                 txt.append(name)

  13. FileToTxt('查找的目录目录',txt)

  14. print(txt)
复制代码


这个是我写的一个类似功能的。如果用字典存在相同的键,不合适,用列表是不是也满足
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-3 11:27:18 | 显示全部楼层
  1. import os
  2. def f(path,txt):
  3.     for each in os.listdir(path):
  4.         (name,extension) = os.path.splitext(each)
  5.         if extension == '.txt':
  6.             txt[name] = each      
  7.         elif extension == '':
  8.             f(os.path.join(path,name),txt)

  9. path = r'c:\py'
  10. txt = dict()
  11. f(path,txt)
  12. print(txt)
复制代码


05行字典的更新,不是这样的。
  1.         if os.path.isdir(os.path.join(path,each)):
  2.             f(os.path.join(path,each),txt)
复制代码

这样进入子目录也可以。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-3 14:16:28 | 显示全部楼层
堕落之翼 发表于 2017-10-2 23:15
这个是我写的一个类似功能的。如果用字典存在相同的键,不合适,用列表是不是也满足

谢谢堕落之翼,我其他方法也做出来了,我的做法是:在字典里用路径作为键,txt作为值,键可以以对应多个值.
我用了内置的BIF中的os.walk(),程序如下:
  1. def search_txt(txt):
  2.     for (path,folder,file) in os.walk(os.getcwd()):
  3.         for each in file:
  4.             (name,extension) = os.path.splitext(each)
  5.             if extension == '.txt':
  6.                 if path not in txt: txt[path] = []
  7.                 txt[path].append(each)  #将相对路径作为键,文件作为值
复制代码

这是搜索存放该.py的文件夹以及子文件夹下的txt文件.
我想问的是:为什么自己用递归的时候会有错误.出现错误的原因是什么.
再次感谢你的参与回答呐,非常感谢!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-3 14:22:54 | 显示全部楼层
bush牛 发表于 2017-10-3 11:27
05行字典的更新,不是这样的。

这样进入子目录也可以。

谢谢bush牛,我的目的是:在字典里用路径作为键,txt作为值,键可以以对应多个值.
我说得有些漏洞,第一次发帖子没将问题说得更具体一些,不好意思.
我想问的是:为什么自己用递归的时候会有错误.出现错误的原因是什么.
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-10-3 17:18:45 | 显示全部楼层    本楼为最佳答案   
  1. import os

  2. def f(path, txt):
  3.         for each in os.listdir(path):
  4.                 each = os.path.join(path,each)
  5.                 (name,extension) = os.path.splitext(each)
  6.                 if extension == '.txt':
  7.                         print(each)
  8.                 if os.path.isdir(each):
  9.                         f(os.path.join(path,each), txt)

  10. def ff(path, txt):
  11.         for each in os.listdir(os.curdir):
  12.                 (name,extension) = os.path.splitext(each)
  13.                 if extension == '.txt':
  14.                         print(each)
  15.                 if os.path.isdir(each):
  16.                         os.chdir(os.path.join(os.curdir,each))
  17.                         ff(os.path.join(os.curdir,each), txt)
  18.         os.chdir(os.pardir)
  19.        
  20. path = os.getcwd()
  21. txt = dict()
  22. f(path, txt)
  23. print('============')
  24. ff(path, txt)

复制代码


我想你应该是path 和os.curdir 混合着用了。你用for each in os.listdir(os.curdir)遍历当前目录下的所有文件,但是你调用函数的时候并没有改变当前的目录。这是我修改后的,你可以看看哈~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-10-3 19:20:29 | 显示全部楼层
啊哈123 发表于 2017-10-3 17:18
我想你应该是path 和os.curdir 混合着用了。你用for each in os.listdir(os.curdir)遍历当前目录下的所 ...

一语惊醒梦中人,非常感谢啊哈123,真的很感谢呐.
这是我自己修改后的,有兴趣可以运行运行呐!
  1. import os

  2. def f(path,txt):
  3.     os.chdir(path)
  4.     for each in os.listdir(os.curdir):
  5.         (name,extension) = os.path.splitext(each)
  6.         if extension == '.txt':
  7.             if path not in txt:txt[path] = []      #将相对路径作为键,txt文件名作为值
  8.             else:txt[path].append(each)
  9.         if os.path.isdir(each):
  10.             f(os.path.join(path,each),txt)
  11.             os.chdir(os.pardir)     #返回上一级目录

  12. path = os.getcwd()
  13. txt = dict()
  14. f(path,txt)
  15. print(txt)
复制代码

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-2 20:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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