码迷,mamicode.com
首页 > 编程语言 > 详细

Python递归实现遍历目录

时间:2018-08-10 15:49:51      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:users   import   print   file   os.path   内容   div   join   打开文件   

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递归实现遍历目录

标签:users   import   print   file   os.path   内容   div   join   打开文件   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!