【递归遍历03】队列模拟遍历目录(广度遍历)
程序员文章站
2022-05-03 20:05:15
...
import os
import collections
def getAllDir(path):
queue = collections.deque()
queue.append(path)
# 当栈为空的时候处理栈
while len(queue) != 0:
# 从队列里取数据
dirpath = queue.popleft()
filelist = os.listdir(dirpath)
# 处理文件
for filename in filelist:
fileAbsPath = os.path.join(dirpath, filename)
# print(fileAbsPath)
if os.path.isdir(fileAbsPath):
print('目录', filename, '入队')
queue.append(fileAbsPath)
else:
print('文件', filename)
path = r'D:\pypypy\high_learn'
getAllDir(path)
先入队,先处理
爬虫会用到!