python精练(1)-创新互联
#/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.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
创新互联建站是一家专业提供梁园企业网站建设,专注与网站设计制作、成都网站建设、HTML5建站、小程序制作等业务。10年已为梁园众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。名称栏目:python精练(1)-创新互联
文章源于:http://ybzwz.com/article/doiide.html