鱼C论坛

 找回密码
 立即注册
查看: 714|回复: 1

文件系统

[复制链接]
发表于 2019-5-17 13:29:49 | 显示全部楼层 |阅读模式

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

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

x
import os
def search_file(path, target):
    os.chdir(path)
    for each_file in os.listdir(os.curdir):
        ext = os.path.splitext(each_file)[1]
        if ext in target:
            video_list.append(os.getcwd() + os.sep + each_file + os.linesep)
        if os.path.isdir(each_file):
            search_file(each_file, target)
            os.chdir(os.pardir)
path = input('请输入需要检索的路径:')
program_dir = os.getcwd()
video_list = []
target = ['avi', 'rmvb', 'jpg', 'png']
search_file(path, target)
f = open(program_dir + os.sep + 'vediolist1.txt', 'w')
f.writelines(video_list)
f.close()

我找了好多遍真的找不到bug,怎么建立的txtl里没东西。求大佬指点!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-5-17 14:16:04 | 显示全部楼层
本帖最后由 jackz007 于 2019-5-17 15:42 编辑

    试试这个代码:
  1. import os
  2. def search_file(path , target):
  3.     try:                                                   # 错误捕获
  4.         for each_file in os . listdir(path):               # 风险语句,当路径 path 无权限访问时,执行该语句会发生错误
  5.             x = os . path . join(path , each_file)         # 如果风险语句执行顺利,就从这里继续,否则就跳去 except: 块继续执行
  6.             if os . path . isfile(x):
  7.                 ext = os . path . splitext(each_file)[1]
  8.                 if ext . lower() in target:
  9.                     video_list . append(x)
  10.             elif os . path . isdir(x):
  11.                 search_file(x , target)
  12.     except:                                                # 错误捕获,如果风险语句出现错误就会立即跳到这里继续执行
  13.         pass                                               # 什么都不做

  14. video_list = []
  15. target = ('.avi', '.rmvb', '.jpg', '.png')
  16. path = input('请输入需要检索的路径:')
  17. search_file(path , target)
  18. with open('vediolist1 . txt' , 'wt') as f:
  19.     for x in video_list:
  20.         f . write(x + '\n')
  21.         print(x)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-15 20:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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