栈的使用

import os
import collections

#通过栈遍历目录(深度遍历)
def test(path):
stack=[]
stack.append(path)
#处理栈
while len(stack)!=0:
dirpath=stack.pop()
filelist=os.listdir(dirpath)
for filename in filelist:
fileAbsPath=os.path.join(dirpath,filename)
if os.path.isdir(fileAbsPath):
stack.append(fileAbsPath)
else:
print("文件:"+filename)

path=r"C:\Users\HP\Desktop\python基础知识"
test1(path)

猜你喜欢

转载自www.cnblogs.com/wfw001-2018/p/10290479.html