Python栈模拟深度遍历

encoding:utf-8

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

import os

def getAll(path):
stack = [] #栈
stack.append(path)

#出力栈  取数据
while len(stack) != 0:
    #出栈
    dirPath = stack.pop()
    #目录下边的文件
    list1 = os.listdir(dirPath)
    # print(list1)
    for filePath in list1:
        #拼接路径
        fileAbs = os.path.join(dirPath,filePath)
        if os.path.isdir(fileAbs):
            #是目录入栈
            print('目录',fileAbs)
            stack.append(fileAbs)
        else:
            print('文件',fileAbs)

你可能感兴趣的:(Python栈模拟深度遍历)