`
sls0919
  • 浏览: 123099 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

递归遍历目录

阅读更多

下面是一个递归方法遍历目录,显示目录树的例子

#encoding=utf-8

import os
import sys

def main(path,depth=0):
    print depth * '|' + '|_',os.path.basename(path)
    if os.path.isdir(path):
        for item in os.listdir(path):
            newpath = path + '/' + item
            newdepth = depth + 1
            main(newpath,newdepth)

if __name__ == '__main__':
    main(sys.argv[1])
 
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics