|
|
发表于 2019-5-1 13:35:22
|
显示全部楼层
这一行有问题:
- for each_file in os.listdir():
复制代码
应该改成这样:
- for each_file in os.listdir(os.getcwd()):
复制代码
我把楼主的代码优化了一下,可以自动跨过没有访问权限的目录,使用不受限制,楼主可以测试一下。
- import os
- def func(open_file , type_list):
- try:
- for each_file in os . listdir(open_file):
- x = os . path . join(open_file , each_file)
- if os . path . isfile(x) :
- ext = os . path . splitext(each_file)[1] . lower()
- if ext in type_list :
- video_list . append(x)
- elif os . path . isdir(x) :
- func(x , type_list)
- except:
- pass
- open_file = input('请输入要查找的文件目录:')
- video_list = []
- func(open_file , ('.avi' , '.rmvb' , '.mp4' , '.mkv' , '.flv' , '.wmv'))
- txt = os . path . join(open_file , 'videolist.txt')
- with open(txt , 'w') as f:
- for x in video_list :
- print(x)
- f . write(x + '\n')
复制代码 |
|