码迷,mamicode.com
首页 > 其他好文 > 详细

74-递归函数2:tree功能显示

时间:2019-06-03 22:17:15      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:name   director   art   color   imp   error   eth   bsp   目录结构   

使用递归函数实现tree功能显示目录结构:

import os
import sys

def list_files(path):
    if os.path.isdir(path):
        print(path + :)
        content = os.listdir(path)
        print(content)
        for fname in content:
            fname = os.path.join(path, fname)
            list_files(fname)

if __name__ == __main__:
    list_files(sys.argv[1])   # python3 dir.py /data/weblog

结果输出:

hejianping@VM-0-2-ubuntu:~$ python dir.py /data/weblog
/data/weblog:
[nginx]
/data/weblog/nginx:
[my-sweetheart.cn.error.log, www.test.com.error.log, www.test.com.access.log, my-sweetheart.cn.access.log]
hejianping@VM-0-2-ubuntu:~$ tree /data/weblog/
/data/weblog/
└── nginx
    ├── my-sweetheart.cn.access.log
    ├── my-sweetheart.cn.error.log
    ├── www.test.com.access.log
    └── www.test.com.error.log

1 directory, 4 files
hejianping@VM-0-2-ubuntu:~$ 

 

74-递归函数2:tree功能显示

标签:name   director   art   color   imp   error   eth   bsp   目录结构   

原文地址:https://www.cnblogs.com/hejianping/p/10969928.html

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