python中递归函数查看目录

import os#os模块

def getAllDir(path,sp=""):
#得到当前目录下所有的文件
filesList=os.listdir(path)

#出来每一个文件
sp+=" "#打印一个空格
for fileName in filesList:
#判断是否是路径(绝对路径)
fileAbspath=os.path.join(path,fileName)
#判断绝对路径下是不是目录
if os.path.isdir(os.path.join(path,fileName)):
#打印目录名
print(sp,"目录:",fileName)#打印空格使得有层级感
#递归调用
getAllDir(fileAbspath,sp)
else:
print(sp,"普通文件:",fileName)

getAllDir(r"D:\f\Python\pycharm\1234")

猜你喜欢

转载自www.cnblogs.com/zlong123/p/10436485.html