Python递归查找文件(os.path, fnmatch,glob)

#!/usr/bin/env python
#coding=utf8

import os.path
import fnmatch
import glob

def recursiveSearchFiles(dirPath, partFileInfo): 
    fileList = []
    pathList = glob.glob(os.path.join('\\', dirPath, '*'))#windows path
    #print 'pathList = '
    #print pathList
    for mPath in pathList:
        #print mPath
        if fnmatch.fnmatch(mPath, partFileInfo):
            fileList.append(mPath) #符合条件条件加到列表
        elif os.path.isdir(mPath):
            #print mPath
            fileList += recursiveSearchFiles(mPath, partFileInfo) #将返回的符合文件列表追加到上层
        else:
            pass
    return fileList
    

path = recursiveSearchFiles("C:\\Users\\Administrator\\Desktop\\", "*.py")#windows path
print path

posted @ 2015-10-09 15:17  笑面浮屠  阅读(677)  评论(0编辑  收藏  举报