首页 > 编程语言 > 详细

Python递归实现遍历目录

时间:2018-08-10 15:00:30      阅读:179      评论:0      收藏:0      [点我收藏+]
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("\t" * n, el)
            read(fp, n + 1)  # 又是文件夹. 继续读取内部的内容 递归入口
        else:
            print("\t" * n, el)  # 递归出口


read(filePath, 0)

 

Python递归实现遍历目录

原文:https://www.cnblogs.com/xiao-xue-di/p/9454976.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!