首页 > 编程语言 > 详细

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

时间:2020-02-08 14:45:32      阅读:61      评论:0      收藏:0      [点我收藏+]
import os
import collections

def getAllDirQU(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)
else:
print("普通文件:" + fileName)
getAllDirQU("D:\python")

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

原文:https://www.cnblogs.com/pygo/p/12275919.html

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