import os
from collections import deque
path=r"C:\aa"
queue=deque([]) #队列
queue.append(path)
while len(queue)!=0:
path=queue.popleft() #从队列取出值
filelist=os.listdir(path) #遍历路径
for filename in filelist:
filepath=os.path.join(path,filename)
if os.path.isdir(filepath):
print("文件夹",filename)
queue.append(filepath) #加入队列
else:
print("文件", filename)
Python 用队列,广度遍历文件夹
最新推荐文章于 2021-01-29 21:02:36 发布