您的位置:首页 > 编程语言 > Python开发

Python 用队列,广度遍历文件夹

2017-10-12 16:36 239 查看
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