python 遞歸遍歷目錄


import os

def getAllDirRE(path, sp = ""):
#得到當前目錄下所有的文件
filesList = os.listdir(path)
#處理每一個文件
sp += " "
for fileName in filesList:
#判斷是否是路徑(用絕對路徑)
fileAbsPath = os.path.join(path, fileName)
if os.path.isdir(fileAbsPath):
print(sp + "目錄:", fileName)
#遞歸調用
getAllDir(fileAbsPath, sp)
else:
print(sp + "普通文件:", fileName)
getAllDirRE("D:\python")


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM