Python广度遍历文件(利用队列的先进先出思想,实现文件的广度遍历)
import os import collections def getAllDirQuene(path): quene = collections.deque() quene.append(path) while len(quene) != 0: dirPath = quene.pop() fileList = os.listdir(dirPath) for fileName in fileList: fileAbsPath = os.path.join(dirPath, fileName) if os.path.isdir(fileAbsPath): print('folder:',fileName) quene.append(fileAbsPath) else: print('file:',fileAbsPath) #调用方法 getAllDirQuene(r"C:\Users\***\PycharmProjects\pythonProject")