def find_file(path='.', strs=''):
try:
current_path = os.listdir(path)
for a in current_path:
if os.path.isfile(os.path.join(path, a)):
if strs in a:
print("You search str is %s, the file at %s" % (strs, os.path.join(path, a)))
else:
pass
elif os.path.isdir(os.path.join(path, a)):
path1 = os.path.join(path, a)
find_file(path1, strs)
else:
print("unkonw type!")
except Exception as e:
print(e)
用户5963807916
coding:utf-8
import os
2、当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径。
def find_file(path='.', strs=''): try: current_path = os.listdir(path) for a in current_path: if os.path.isfile(os.path.join(path, a)): if strs in a: print("You search str is %s, the file at %s" % (strs, os.path.join(path, a))) else: pass elif os.path.isdir(os.path.join(path, a)): path1 = os.path.join(path, a) find_file(path1, strs) else: print("unkonw type!") except Exception as e: print(e)
find_file('D:/', 'test')