python 精练(1)

发布时间:2020-07-03 03:40:35 作者:运维天空
来源:网络 阅读:411
#/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()主方法。
推荐阅读:
  1. python基础1
  2. Python操作MySQL(1)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python path sep

上一篇:android基础三之CheckBox

下一篇:若何进修单片机

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》