Python递归实现遍历目录

Posted 小学弟-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python递归实现遍历目录相关的知识,希望对你有一定的参考价值。

import os

filePath = "/Users/busensei/wzy/filePath/"


def read(filePath, n):
    it = os.listdir(filePath)  # 打开文件夹
    for el in it:
        #  拿到路径
        fp = os.path.join(filePath, el)  # 获取到绝对路径
        if os.path.isdir(fp):  # 判断是否是文件夹
            print("	" * n, el)
            read(fp, n + 1)  # 又是文件夹. 继续读取内部的内容 递归入口
        else:
            print("	" * n, el)  # 递归出口


read(filePath, 0)

 

以上是关于Python递归实现遍历目录的主要内容,如果未能解决你的问题,请参考以下文章