python实现查找指定文件

若不包含子目录的遍历:

import glob

for filename in glob.glob("/data/testdata/*.jpg"):

    print filename

包含子目录

import os

import fnmatch

def iterfindfiles(path, fnexp):

    for root, dirs, files in os.walk(path):

        for filename in fnmatch.filter(files, fnexp):

            yield os.path.join(root, filename)

for filename in iterfindfiles(r"/data/testdata", "*.jpg"):

    print filename

yield 用法讲解 http://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/

原文地址:https://www.cnblogs.com/weiok/p/4872119.html