《课后作业三十讲》第3小题对于自己所写答案的疑惑
import osdef search(start_path):
os.chdir(start_path)
namelst = os.listdir(os.curdir)
extension = ['.mp4','.rmvb','.avi']
for each_file in namelst:
if os.path.isfile(each_file):
if os.path.splitext(each_file) in extension:
f = open('D:\\vedioList.txt','x')
final_path = start_path + each_file
f.writelines(final_path)
f.close()
if os.path.isdir(each_file):
search(each_file)
os.chdir(os.pardir)
start_path = input('请输入待查找的初始目录:')
search(start_path)
在运行过后没有报错也没有结果,这是为什么呢?{:5_94:}
还有一个小问题:小甲鱼的片名能搜到嘛? 本帖最后由 jackz007 于 2021-2-6 18:33 编辑
import os
def search(start_path):
pwd = os . getcwd()
os . chdir(start_path)
cwd = os . getcwd()
extension = ['.mp4','.mkv' ,'.flv' , '.rmvb' , '.rm' , '.wmv' , '.avi']
try:
for each_file in os.listdir(os . curdir):
if os . path . isfile(each_file):
if os . path . splitext(each_file) . lower() in extension:
f = open('D:\\vedioList.txt','a')
final_path = os . path . join(cwd , each_file)
f . write(final_path + '\n')
f . close()
elif os . path . isdir(each_file):
search(each_file)
except Exception as e:
print(e)
os . chdir(pwd)
start_path = input('请输入待查找的初始目录:')
if os . path . exists('D:\\vedioList.txt') andos . path . isfile('D:\\vedioList.txt'):
os . remove('D:\\vedioList.txt')
search(start_path)
页:
[1]