美文网首页
Python队列模拟深度遍历

Python队列模拟深度遍历

作者: 暖遇 | 来源:发表于2018-09-11 16:38 被阅读0次

encoding:utf-8

author = 'zhoupao'
date = '2018/7/14 10:17'

import os
import collections

def getAll(spath):
if not os.path.exists(spath):
print('没有该目录')
#创建一个队列
queue=collections.deque()
# 将spath放入队列中
queue.append(spath)

while len(queue) !=0:
    # 从顶部 弹出
    firstPath=queue.popleft()
    # 查看里面所有的文件
    innerPath=os.listdir(firstPath)

    for listDir in innerPath:
        allPath=os.path.join(innerPath,listDir)

        if os.path.isdir(allPath):
            print('是目录',allPath)
            queue.append(allPath)
        else:
            print('是文件',allPath)

相关文章

网友评论

      本文标题:Python队列模拟深度遍历

      本文链接:https://www.haomeiwen.com/subject/uubugftx.html