最简短的遍历显示当前目录树的python方法,当前目录树python,遍历显示当前目录树可以用


遍历显示当前目录树可以用多种方式来实现,今天贴的是最简短又实用的方法。python语言简洁优美,当然要用最少的代码,完成目标任务才是最优。

最简短的遍历显示当前目录树的python方法如下:

from os.path import basename, isdirfrom os import listdirdef traverse(path, depth=0):    print depth* '| ' + '|_', basename(path)    if(isdir(path)):        for item in listdir(path):            traverse(path+'/'+item, depth+1)if __name__ == '__main__':    traverse('./')

编橙之家文章,

评论关闭