说明
本文介绍 获取指定目录下,所有的子文件、子文件夹中的文件
注意:不要子文件夹
缺点
如果要遍历所有子文件夹、子文件,参见:
python 遍历所有的子文件夹、子文件-os.scandir()如果要实现过滤,性能不高,有点复杂,可以考虑:
python 遍历所有的子文件夹、子文件-os.scandir()
path.rglob()
from pathlib import Path
path = Path(r"C:\Users\mym\Desktop")
for file in path.rglob("*.*"):
print(file)
os.walk
import os
path = r"C:\Users\mym\Desktop\重庆发票"
for root,folders,files in os.walk(path):
for file in files:
print(os.path.join(root,file))
参考:
https://zhuanlan.zhihu.com/p/609327324