【Python】【笔记】 查找该路径下(包含子文件夹内)所有的某格式文件
import osdef find_ext(start_dir , ext_list):
os.chdir(start_dir)
cur_dir = os.listdir(os.getcwd())
for each_file in cur_dir:
if os.path.splitext(each_file) in ext_list:
result_list.append(os.getcwd() + os.sep + each_file + os.linesep)# 使用os.sep是程序更标准
if os.path.isdir(each_file):
find_ext(each_file , ext_list)# 递归调用
os.chdir(os.pardir)# 递归调用后切记返回上一层目录
ext = ['.avi','.mp4','.rmvb','.MP4','.flv','.mkv']
dir = r'F:鱼c光盘合集'
result_list = []
find_ext(dir , ext)
with open('result.txt','w') as f:
f.writelines(result_list)
页:
[1]