import osdef find(name, path): for root, dirs, files in os.walk(path): if name in files: return os.path.join(root, name)
这将找到所有匹配项:
def find_all(name, path): result = [] for root, dirs, files in os.walk(path): if name in files: result.append(os.path.join(root, name)) return result
这将匹配一个模式:
import os, fnmatchdef find(pattern, path): result = [] for root, dirs, files in os.walk(path): for name in files: if fnmatch.fnmatch(name, pattern): result.append(os.path.join(root, name)) return resultfind('*.txt', '/path/to/dir')
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)