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

Python栈模拟深度遍历

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

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栈模拟深度遍历

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