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

python - 输出目录结构

时间:2020-04-23 00:40:16      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:highlight   目录   lis   charm   har   spl   list   arm   imp   

  感觉这个没什么好说的:

import os


def rev(path: str, i=0):
    s = ‘+‘ * i + path.split(‘/‘)[-1] + ‘\n‘
    for _ in os.listdir(path):
        if os.path.isdir(path + ‘/‘ + _):
            s += rev(path + ‘/‘ + _, i + 1) + ‘\n‘
        elif os.path.isfile(path + ‘/‘ + _):
            s += ‘+‘ + ‘+‘ * i + _ + ‘\n‘
    return s


print(rev(‘D:/PycharmProjects‘))

  

python - 输出目录结构

标签:highlight   目录   lis   charm   har   spl   list   arm   imp   

原文地址:https://www.cnblogs.com/darkchii/p/12757890.html

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