python递归遍历

来源:互联网 发布:华为端口undo dcn 编辑:程序博客网 时间:2024/04/30 22:52

01 #listdir.py
02 import os
03 # 递归遍历指定的目录
04 # level -- 递归的层数,用这个参数来控制打印的缩进
05 # path == 遍历起始绝对路径
06 def listyoudir(level, path):
07 for i in os.listdir(path):
08 print ' '*(level+1) + i
09 if os.path.isdir(path + '//' + i):
10 listyoudir(level+1, path + '//' + i)
11
12 #测试代码
13 rootpath = os.path.abspath('.')
14 print rootpath
15 listyoudir(0, rootpath)