下面是一个递归方法遍历目录,显示目录树的例子
#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])