defwalk(dirs): files_all = [] for root, d, files in os.walk(dirs): for file in files: file_path = os.path.join(root, file) files_all.append(file_path) return files_all
1 2 3 4 5 6 7 8
defscan(dirs): files_all = [] for item in os.scandir(dirs): if item.is_file(): files_all.append(item.path) if item.is_dir(): # 对于目录,递归检索文件 files_all += scan(dirs=item.path) return files_all
1 2
path = "data" set(walk(path)) == set(scan(path))
True
1 2
%%timeit -n 1000 walk(path)
281 µs ± 67 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
1 2
%%timeit -n 1000 scan(path)
165 µs ± 2.72 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)