Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

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

QQ截图20200721185820.png
寻求大佬指点一下,主要存在的问题就是以字典的形式输出


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

用py的pathlib库

from pathlib import Path

def 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)  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...