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")
posted @ 2019-02-26 12:26  飞飞阿  阅读(258)  评论(0编辑  收藏  举报