首页 > 其他 > 详细

队列模拟递归遍历目录(广度遍历)

时间:2020-02-19 16:44:59      阅读:57      评论:0      收藏:0      [点我收藏+]
import os
import collections
def getALLDir(path):
    queue = collections.deque()#空的
    queue.append(path)#进队

    while len(queue) != 0:
        dirPath = queue.popleft()#数据出队
        filesList = os.listdir(dirPath)#找出所有文件

        for fileName in filesList:#处理文件
            fileAbsPath = os.path.join(dirPath,fileName)#合成文件绝对路径
            if os.path.isdir(fileAbsPath):#判断文件类型
                print("目录:" + fileName)
                queue.append(fileAbsPath)#若文件为目录 则进队 进行!=0的判断 即继续遍历
            else:
                print("普通文件:" + fileName)
getALLDir(r"C:\Users\23678\Desktop\超星智慧树网课助手浏览器版V1.1.0")

 

思路简图

画的有点丑,不要嫌弃 (~ ̄▽ ̄)~

 

 

技术分享图片

 

队列模拟递归遍历目录(广度遍历)

原文:https://www.cnblogs.com/FSHOU/p/12331774.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!