- Python语言: python.递归遍历指定的目录
- #listdir.py
- import os
- # 递归遍历指定的目录
- # level -- 递归的层数,用这个参数来控制打印的缩进
- # path == 遍历起始绝对路径
- def listyoudir(level, path):
- for i in os.listdir(path):
- print ' '*(level+1) + i
- if os.path.isdir(path + '\\\\' + i):
- listyoudir(level+1, path + '\\\\' + i)
- #测试代码
- rootpath = os.path.abspath('.')
- print rootpath
- listyoudir(0, rootpath)
- #该片段来自于http://www.codesnippet.cn/detail/260220132222.html
来源: http://www.codesnippet.cn/detail/260220132222.html