使用队列来遍历目录
import collections
import os
def getAllDirQueue(path):
# 创建队列
queue = collections.deque()
queue.append(path)
while queue:
# 出队
filepath = queue.popleft()
fileList = os.listdir(filepath)
for filename in fileList:
absPath = os.path.join(filepath,filename)
if os.path.isdir(absPath):
print("目录:",filename)
queue.append(absPath)
else:
print("文件:",filename)
#输入路径
path = r"/Users/Pycharm"
getAllDirQueue(path)