1. 程式人生 > >python中用隊列模擬遞歸(廣度遍歷)

python中用隊列模擬遞歸(廣度遍歷)

all 是否 arm while abs files pytho 隊列 lec

用隊列模擬遞歸(廣度遍歷)
import os
import collections

def getAllDirQU(path):
queue=collections.deque()
#進隊
queue.append(path)

while len(queue)!=0:
#出隊數據
dirPath=queue.popleft()
#找出dirPath所有的文件
filesList=os.listdir(dirPath)

for fileName in filesList:
#絕對路徑
fileAbsPath=os.path.join(dirPath,fileName)
#判斷是否是目錄,是目錄就進隊,不是就打印
if os.path.isdir(fileAbsPath):
print("目錄:"+fileName)
else:
print("普通文件:"+fileName)

getAllDirQU(r"D:\f\Python\pycharm\234")

python中用隊列模擬遞歸(廣度遍歷)