栈结构遍历

import os
def alldir(path):
stack=[]
stack.append(path)
while len(stack)!=0:
dirpath=stack.pop()
filelist=os.listdir(dirpath)
for filename in filelist:
fileabs=os.path.join(dirpath,filename)
if os.path.isdir(fileabs):
stack.append(fileabs)
print('目录:'+filename)
else:
print('文件:'+filename)
alldir(r'C:\Program Files (x86)')

转载于:https://www.cnblogs.com/menxin/p/9823610.html

你可能感兴趣的:(栈结构遍历)