目录遍历--递归函数

#!/usr/bin/env python
# coding:UTF-8


"""
@version: python3.x
@author:曹新健
@contact: [email protected]
@software: PyCharm
@file: 目录遍历.py
@time: 2018/9/6 16:53
"""

import os
def getAllFiles(path,filler='    ',spfiller=''):
    dirList = os.listdir(path)
    spfiller += filler
    print(spfiller)

    for dirname in dirList:
        curpath = os.path.join(path,dirname)
        if os.path.isdir(curpath):
            print(spfiller + dirname)
            getAllFiles(curpath,spfiller=spfiller)
        else:
            print(spfiller + dirname)


if __name__ == "__main__":
    getAllFiles(r'E:\学习')

猜你喜欢

转载自blog.csdn.net/caoxinjian423/article/details/77503719