python 枚举目录下所有子目录和文件,输出列表

#WalkDir函数枚举目录中所有目录和文件,参数(枚举目录输出列表,枚举文件输出列表,要枚举的目录)
def WalkDir(dirlist,filelist,dirname):
    try:
        ls=os.listdir(dirname)
    except:
        print "Access Deny."
    else:
        for fn in ls:
            temp=os.path.join(dirname,fn)
            if (os.path.isdir(temp)):
                dirlist.append(temp)
                WalkDir(dirlist,filelist,temp)
            else:
                filelist.append(temp)
    return dirlist,filelist

 

posted @ 2014-10-22 22:10  arhatlohan  阅读(2337)  评论(0编辑  收藏  举报