import os
deffind_file(path):for i in os.listdir(path):# print(path + '\\' + i) #输出文件和目录及路径try:if os.path.isdir(path +'\\'+ i):
find_file(path +'\\'+ i)else:print(path +'\\'+ i)#只输出文件及路径 except Exception as e:print(e)
find_file(r'd:\wsq')#'r'防止python中"\"转译字符,(you can use '\\' or '/' replace it,also)
2.查找指定模糊路径指定文件的所在绝对路径
import os
deffind_file_path(path,file_name):
file_list =[]for i in os.listdir(path):
wsq = path +'\\'+ i
try:if os.path.isdir(wsq):
find_file_path(wsq,file_name)else:# if file_name == i: #字符串精确匹配if file_name in i:#字符串模糊匹配 print(path +'\\'+ i)except Exception as e:print(e)
filePath =input("please input filePath:")
fileName =input("please input fileName:")
find_file_path(filePath,fileName)