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)

作者:wenhai_zhang 发表于2009-12-14 20:10:00 原文链接
阅读:92 评论:0 查看评论
posted @ 2009-12-14 12:10  文海  阅读(659)  评论(0编辑  收藏  举报