首页 > 编程语言 > 详细

python 栈模拟递归遍历目录(深度遍历)

时间:2020-02-08 13:50:01      阅读:60      评论:0      收藏:0      [点我收藏+]
import os

def getAllDirDE(path):
stack = []
stack.append(path)

#处理栈,当栈为空的时候结束循环
while len(stack) != 0:
#从栈里取出数据
#[]
dirPath = stack.pop()
#print(dirPath)
#目录下所有文件
filesList = os.listdir(dirPath)
#print(filesList)
#处理每一个问价,如果是普通文件则打印出来,如果是目录则将该目录的地址压栈
for fileName in filesList:
fileAbsPath = os.path.join(dirPath, fileName)
if os.path.isdir(fileAbsPath):
#是目录就压栈
print("目录:" + fileName)
stack.append(fileAbsPath)
#["B", "E", "F"]
else:
#打印普通文件
print("普通:" + fileName)
getAllDirDE("D:\python")

python 栈模拟递归遍历目录(深度遍历)

原文:https://www.cnblogs.com/pygo/p/12275909.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!