import os
def finder(pattern, root='.'):
matches = []
dirs = []
for x in os.listdir(root):
nd = os.path.join(root, x)
if os.path.isdir(nd):
dirs.append(nd)
elif os.path.isfile(nd) and pattern in x:
matches.append(nd)
for match in matches:
print(match)
for dir in dirs:
finder(pattern, root=dir)
finder('First',root='/home')
比如我要找 /home 文件夹下的所有名字包含‘First’的文件: