|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
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 = ['.txt', '.py', '.rmvb']
vedio_list = []
search_file(start_dir, target)
f = open(program_dir + os.sep + '文件集合.txt', 'w')
f.writelines(vedio_list)
f.close()
问题:1、在《文件集合.txt》只写入了我这个py文件所在目录的文件名,和初始目录也没有关系
2、如果该目录下有文件夹,这里无法进入文件夹下搜索,并写入文件内,也就是说第二个if没有执行,这是为啥 |
|