赞
踩
在处理数据的过程中,经常需要遍历文件夹,如果远程服务器的文件是分布式存储,遍历需要更快的速度。
实测pathlib.Path().rglob()方案要快于os.walk()方案。
os.walk的遍历方式,支持筛选后缀和排序:
def traverse_dir_files(root_dir, ext=None, is_sorted=True):
"""
列出文件夹中的文件, 深度遍历
:param root_dir: 根目录
:param ext: 后缀名
:param is_sorted: 是否排序,耗时较长
:return: [文件路径列表, 文件名称列表]
"""
names_list = []
paths_list = []
for parent, _, fileNames in os.walk(root_dir):
for name in fileNames:
if name.startswith('.'): # 去除隐藏文件
continue
if ext: # 根据后缀名搜索
if name.endswith(tuple(ext)):
names_list.append(name)
paths_list.append(os.path.join(parent, name))
else:
names_list.append(name)
paths_list.append(os.path.join(parent, name))
if not names_list: # 文件夹为空
return paths_list, names_list
if is_sorted:
paths_list, names_list = sort_two_list(paths_list, names_list)
return paths_list, names_list
pathlib.Path().rglob()的遍历方式,也支持筛选后缀和排序:
def traverse_dir_files(root_dir, ext=None, is_sorted=True):
"""
列出文件夹中的文件, 深度遍历
:param root_dir: 根目录
:param ext: 后缀名
:param is_sorted: 是否排序,耗时较长
:return: [文件路径列表, 文件名称列表]
"""
names_list = []
paths_list = []
for path in list(pathlib.Path(root_dir).rglob("*")):
path = str(path)
name = path.split("/")[-1]
if name.startswith('.') or "." not in name: # 去除隐藏文件
continue
if ext: # 根据后缀名搜索
if name.endswith(ext):
names_list.append(name)
paths_list.append(path)
else:
names_list.append(name)
paths_list.append(path)
if not names_list: # 文件夹为空
return paths_list, names_list
if is_sorted:
paths_list, names_list = sort_two_list(paths_list, names_list)
return paths_list, names_list
参考:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。