|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 喜欢小翘臀 于 2016-10-7 20:39 编辑
甲鱼老师的代码是:
import os
def search_file(start_dir, target) :
os.chdir(start_dir)
for each_file in os.listdir(os.curdir) :
ext = os.path.splitext(each_file)[1]
if ext in target :
vedio_list.append(os.getcwd() + os.sep + each_file + os.linesep) # 使用os.sep是程序更标准
if os.path.isdir(each_file) :
search_file(each_file, target) # 递归调用
os.chdir(os.pardir) # 递归调用后切记返回上一层目录
start_dir = input('请输入待查找的初始目录:')
program_dir = os.getcwd()
target = ['.mp4', '.avi', '.rmvb']
vedio_list = []
search_file(start_dir, target)
f = open(program_dir + os.sep + 'vedioList.txt', 'w')
f.writelines(vedio_list)
f.close()
我想问的是:
1,关于vedio list是个空列表,应该算全局变量吧,为什么在定义的函数里能对这个空列表进行修改呢
2 老师定义的函数里,为什么要用两个if?为何不是if else或者if elif?
3 用老师的这个代码会报拒绝访问的错误。我想问下是我的电脑哪里出问题了吗
请输入待查找的初始目录:H:
Traceback (most recent call last):
File "H:\新建文本文档.py", line 20, in <module>
search_file(start_dir, target)
File "H:\新建文本文档.py", line 11, in search_file
search_file(each_file, target) # 递归调用
File "H:\新建文本文档.py", line 11, in search_file
search_file(each_file, target) # 递归调用
File "H:\新建文本文档.py", line 4, in search_file
os.chdir(start_dir)
PermissionError: [WinError 5] 拒绝访问。: 'S-1-5-21-1103617919-1647945974-1758054018-1000' |
|