鱼C论坛

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

[已解决]第30讲 第2,第3题,

[复制链接]
发表于 2019-9-15 21:36:55 | 显示全部楼层 |阅读模式

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

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

x

小甲鱼的答案,如下

  1. import os

  2. def search_file(start_dir, target) :
  3.     os.chdir(start_dir)
  4.    
  5.     for each_file in os.listdir(os.curdir) :
  6.         if each_file == target :
  7.             print(os.getcwd() + os.sep + each_file) # 使用os.sep是程序更标准
  8.         if os.path.isdir(each_file) :
  9.             search_file(each_file, target) # 递归调用
  10.             os.chdir(os.pardir) # 递归调用后切记返回上一层目录

  11. start_dir = input('请输入待查找的初始目录:')
  12. target = input('请输入需要查找的目标文件:')
  13. search_file(start_dir, target)
复制代码


会出现这个问题
  1. Traceback (most recent call last):
  2.   File "D:\小程序\030讲--输入文件名以及开始搜索的路径.py", line 39, in <module>
  3.     search_file(start_dir, target)
  4.   File "D:\小程序\030讲--输入文件名以及开始搜索的路径.py", line 34, in search_file
  5.     search_file(each_file, target) # 递归调用
  6.   File "D:\小程序\030讲--输入文件名以及开始搜索的路径.py", line 28, in search_file
  7.     os.chdir(start_dir)
  8. PermissionError: [WinError 5] 拒绝访问。: 'System Volume Information'
复制代码

最佳答案
2019-9-15 22:38:46
本帖最后由 angtn 于 2019-9-15 22:48 编辑
jazzli03 发表于 2019-9-15 21:51
你是说递归层数不够用了吗


第二题试试这段代码:
  1. import os
  2. def search_for(directoy,target):
  3.     for (root,dirs,files) in os.walk(directoy):
  4.         if target in files:
  5.             print('\n',os.path.join(root,target))


  6. directoy = input('请输入待查找的初始目录:')
  7. target = input('请输入需要查找的目标文件:')
  8. search_for(directoy,target)
复制代码

第三题

  1. import os

  2. def echo(path):

  3.     f = open('vediolist.txt','a')
  4.    
  5.     for (root,dirs,files) in os.walk(path):
  6.         for every in files:
  7.             if '.mp4' in os.path.splitext(every):

  8.                 f.write('%s\n\n' % os.path.join(root,every))
  9.                
  10.             if '.avi' in os.path.splitext(every):

  11.                 f.write('%s\n\n' % os.path.join(root,every))

  12.             if '.rmvb' in os.path.splitext(every):

  13.                 f.write('%s\n\n' % os.path.join(root,every))
  14.                
  15.             if '.wmv' in os.path.splitext(every):

  16.                 f.write('%s\n\n' % os.path.join(root,every))
  17.                   
  18.     f.close()
  19.    

  20. path = input('请输入待查找的初始目录:')
  21. echo(path)
  22. print('打印成功')
  23.             
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-9-15 21:39:03 | 显示全部楼层
第三题,也出现同样的问题
  1. 请输入带查找的初始目录:D:\\
  2. Traceback (most recent call last):
  3.   File "D:/小程序/030讲--查找该路径下所有的视频格式文件.py", line 19, in <module>
  4.     find(path1)
  5.   File "D:/小程序/030讲--查找该路径下所有的视频格式文件.py", line 9, in find
  6.     find(each)
  7.   File "D:/小程序/030讲--查找该路径下所有的视频格式文件.py", line 9, in find
  8.     find(each)
  9.   File "D:/小程序/030讲--查找该路径下所有的视频格式文件.py", line 9, in find
  10.     find(each)
  11.   [Previous line repeated 990 more times]
  12.   File "D:/小程序/030讲--查找该路径下所有的视频格式文件.py", line 5, in find
  13.     os.chdir(path1)
  14. RecursionError: maximum recursion depth exceeded while calling a Python object
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-15 21:39:06 | 显示全部楼层
因为每个盘的 System Volume Information 目录没有权限访问,所以需要避开:
  1. import os


  2. def search_file(start_dir, target):
  3.     try:
  4.         os.chdir(start_dir)

  5.         for each_file in os.listdir(os.curdir):
  6.             if each_file == target:
  7.                 print(os.getcwd() + os.sep + each_file)  # 使用os.sep是程序更标准
  8.             if os.path.isdir(each_file):
  9.                 search_file(each_file, target)  # 递归调用
  10.                 os.chdir(os.pardir)  # 递归调用后切记返回上一层目录
  11.     except:
  12.         pass


  13. start_dir = input('请输入待查找的初始目录:')
  14. target = input('请输入需要查找的目标文件:')
  15. search_file(start_dir, target)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-15 21:42:39 | 显示全部楼层
jazzli03 发表于 2019-9-15 21:39
第三题,也出现同样的问题

是你的 D 盘太多文件了吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-15 21:51:43 | 显示全部楼层
zltzlt 发表于 2019-9-15 21:42
是你的 D 盘太多文件了吗

你是说递归层数不够用了吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-15 21:52:59 | 显示全部楼层
jazzli03 发表于 2019-9-15 21:51
你是说递归层数不够用了吗

把小甲鱼第 30 讲第 3 题答案发上来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-15 21:55:32 | 显示全部楼层
zltzlt 发表于 2019-9-15 21:52
把小甲鱼第 30 讲第 3 题答案发上来
  1. import os

  2. def search_file(start_dir, target) :
  3.     os.chdir(start_dir)
  4.    
  5.     for each_file in os.listdir(os.curdir) :
  6.         ext = os.path.splitext(each_file)[1]
  7.         if ext in target :
  8.             vedio_list.append(os.getcwd() + os.sep + each_file + os.linesep) # 使用os.sep是程序更标准
  9.         if os.path.isdir(each_file) :
  10.             search_file(each_file, target) # 递归调用
  11.             os.chdir(os.pardir) # 递归调用后切记返回上一层目录

  12. start_dir = input('请输入待查找的初始目录:')
  13. program_dir = os.getcwd()

  14. target = ['.mp4', '.avi', '.rmvb']
  15. vedio_list = []

  16. search_file(start_dir, target)

  17. f = open(program_dir + os.sep + 'vedioList.txt', 'w')
  18. f.writelines(vedio_list)
  19. f.close()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-15 21:58:22 | 显示全部楼层
jazzli03 发表于 2019-9-15 21:51
你是说递归层数不够用了吗

可能是文件夹太多导致的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-15 22:38:46 | 显示全部楼层    本楼为最佳答案   
本帖最后由 angtn 于 2019-9-15 22:48 编辑
jazzli03 发表于 2019-9-15 21:51
你是说递归层数不够用了吗


第二题试试这段代码:
  1. import os
  2. def search_for(directoy,target):
  3.     for (root,dirs,files) in os.walk(directoy):
  4.         if target in files:
  5.             print('\n',os.path.join(root,target))


  6. directoy = input('请输入待查找的初始目录:')
  7. target = input('请输入需要查找的目标文件:')
  8. search_for(directoy,target)
复制代码

第三题

  1. import os

  2. def echo(path):

  3.     f = open('vediolist.txt','a')
  4.    
  5.     for (root,dirs,files) in os.walk(path):
  6.         for every in files:
  7.             if '.mp4' in os.path.splitext(every):

  8.                 f.write('%s\n\n' % os.path.join(root,every))
  9.                
  10.             if '.avi' in os.path.splitext(every):

  11.                 f.write('%s\n\n' % os.path.join(root,every))

  12.             if '.rmvb' in os.path.splitext(every):

  13.                 f.write('%s\n\n' % os.path.join(root,every))
  14.                
  15.             if '.wmv' in os.path.splitext(every):

  16.                 f.write('%s\n\n' % os.path.join(root,every))
  17.                   
  18.     f.close()
  19.    

  20. path = input('请输入待查找的初始目录:')
  21. echo(path)
  22. print('打印成功')
  23.             
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-8 09:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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