4

python 遍历目录获取所有文件

 1 year ago
source link: https://xujinzh.github.io/2022/05/27/python-traverse-dir/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

python 遍历目录获取所有文件

2022-05-27technologypython

4 679 1 分钟

利用 Python 进行文件夹遍历有很多种方法,本篇介绍常用的两种方法,这些方法是内建模块中的方法,速度较快。

os.walk 和 os.scandir

两种方法都是在 os 库实现的,分别是 os.walk,os.scandir,其中后者效率更高些。

import os
def walk(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
def scan(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
path = "data"
set(walk(path)) == set(scan(path))
True
%%timeit -n 1000
walk(path)
281 µs ± 67 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
%%timeit -n 1000
scan(path)
165 µs ± 2.72 µs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
感谢支持!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK