栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 精练

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python 精练

#/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]

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()主方法。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/225717.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号