python广度遍历文件夹

import os
from collections import deque

path = r"E:\Python"
queue = deque([])#队列
queue.append(path)

while len(queue) != 0:
    path = queue.popleft()#弹出的值
    filelist = os.listdir(path)#遍历路径
    
    for filename in filelist:
        filepath = os.path.join(path,filename)
        if os.path.isdir(filepath):
            print("文件夹",filename)
            queue.append(filepath)
        else:
            print("文件",filename)
posted on 2019-12-12 22:46  行之间  阅读(259)  评论(0编辑  收藏  举报