import os
import os.path
def search(path,filetype):
dirlist=[]
try:
dirlist=os.listdir(path)
for each in dirlist:
if os.path.isdir(path+"/"+each):
search(path+"/"+each,filetype) ##如果是文件夹就调用自己继续搜索,递归
else:
if os.path.splitext(path+"/"+each)[1]==filetype: ##搜索指定格式的所有文件
filetxt.append(path+"/"+each)
print(path+"/"+each)
except:
pass
return filetxt
inipath=input("请输入初始搜索路径:")
filetype=input("请输入目标文件类型【如.mp4】:")
filetxt=[]
search(inipath,filetype)
f=open('c:/Users/Administrator/Desktop/files.txt','w')
for each in filetxt:
f.write(each)
print("执行到了这一步")
f.close()
|