-1

我的 settings.Mediaroot 包含 abc.xml、xyz.xml、pqr.xml、lmn.xml。我应该能够排除 abc.xml 和 xyz.xml 并只解析 pqr.xml 和 lmn.xml

exfiles=['abc.xml','xyz.xml']

def locatexml(pattern,exfiles=None):
for path, dirs, files in os.walk(settings.MEDIA_ROOT):
    for filename in fnmatch.filter(files, pattern):
        yield os.path.join(path, filename)
4

1 回答 1

2
def locatexml(pattern,exfiles=None):
    for path, dirs, files in os.walk(settings.MEDIA_ROOT):
        for filename in fnmatch.filter(files, pattern):
            if filename not in exfiles:
                yield os.path.join(path, filename)

您还可以set从 exfiles 进行更快的检查

def locatexml(pattern,exfiles=None):
    exfiles = set(exfiles)
    for path, dirs, files in os.walk(settings.MEDIA_ROOT):
        for filename in fnmatch.filter(files, pattern):
            if filename not in exfiles:
                yield os.path.join(path, filename)
于 2013-09-10T05:49:05.387 回答