前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 精练(1)

python 精练(1)

作者头像
py3study
发布2020-01-14 16:10:53
2030
发布2020-01-14 16:10:53
举报
文章被收录于专栏:python3python3
代码语言:javascript
复制
#/usr/bin/python env
from os import listdir , sep 
from os.path import abspath, basename, isdir 
from sys import argv 

def tree(dir, padding, print_files=False):
    print padding[:-1] + '+-' + basename(abspath(dir)) + '/'
    padding = padding + ' '
    files = []
    if print_files:
        files = listdir(dir)
    else:
        files = [ x for x in listdir(dir) if isdir(dir + sep +x)]
    count = 0 
    for file in files:
        count +=1 
        print padding + '|'
        path = dir + sep + file 
        if isdir(path):
            if count == len(files):
                tree(path, padding + ' ', print_files)
            else:
                tree(path, padding + '|', print_files)
        else:
            print padding + '+-' + file 
def usage():
        return '''Usage: %s [-f] <path>
print tree structure of path specified.
options:
-f           print files as well as directories
path      path to process'''  % basename(argv[0])

def main():
    if len(argv) == 1:
        print usage()
    elif len(argv) == 2:
        path = argv[1]
        if isdir(path):
            tree(path, ' ')
        else:
            print 'ERROR: \'' + path + '\' is not a diretory'
    elif len(argv) == 3 and argv[1] == '-f':
        path = argv[2]
        if isdir(path):
            tree(path, ' ', True)
        else: 
            print 'ERROR: \'' + path + ' \' is not   a dirctory'
        else:
            print usage()

if __name= '__main__':
    main()

    #较长的代码必须要有main()主方法。
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-08-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档