Python:目录遍历--栈深度遍历

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


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

import os
from collections import deque
def stackFiles(path):
    stack = deque()
    stack.append(path)

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

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


猜你喜欢

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