Python:目录遍历--队列广度遍历

#!/usr/bin/env python
# coding:UTF-8


"""
@version: python3.x
@author:曹新健
@contact: [email protected]
@software: PyCharm
@file: 广度遍历队列.py
@time: 2018/9/7 13:50
"""

import os
from collections import deque
def queueFiles(path):
    queue = deque()
    queue.append(path)

    while True:
        if len(queue) == 0:
            break
        popFile = queue.popleft()
        #print(popFile)
        for file in os.listdir(popFile):
            newDir = os.path.join(popFile,file)
            if os.path.isdir(newDir):
                print(file)
                queue.append(newDir)
            else:
                print(file)

if __name__ == "__main__":
    queueFiles(r'E:\学习')

猜你喜欢

转载自blog.csdn.net/caoxinjian423/article/details/82832972