python遍历文件子目录并以字典形式输出

寻求大佬指点一下,主要存在的问题就是以字典的形式输出
回答:
用py的pathlib库
python">from pathlib import Pathdef scan_image_file(path):
    result={}
    dirList =  [str(x) for x in Path(path).iterdir() if x.is_dir()]
    for v in dirList:
        # Path('.').glob('*.jpg') 不使用递归
        result[v]=[str(i) for i  in Path(v).glob('**/*.jpg')]
    return result
# 用递归的形式扫描当下文件夹下子目录内的所有jpg文件
res = scan_image_file('.')
print(res)  
以上是 python遍历文件子目录并以字典形式输出 的全部内容, 来源链接: utcz.com/a/158312.html

