python遍历文件夹

发表于:2007-05-26来源:作者:点击数: 标签:
根据别人的算法用python实现

#!/usr/bin/python

from os.path import basename, isdir
from os import listdir

def traverse(path, depth=0):
 prefix =  depth* '| ' + '|_'
 if(isdir(path)):
  print prefix, basename(path)
  for item in listdir(path):
   traverse(path+'/'+item, depth+1)
 else:
  print prefix, basename(path)

if __name__ == '__main__':
 traverse('./')

原文转自:http://www.ltesting.net