More Efficient fnmatch.fnmatch for multiple patterns?

Wojciech Muła wojciech_mula at poczta.null.onet.pl.invalid
Mon Jan 8 13:55:10 EST 2007


abcd wrote:
> I am using fnmatch.fnmatch to find some files.  The only problem I have
> is that it only takes one pattern...so if I want to search using
> multiple patterns I have to do something like....
>
> patterns = ['abc*.txt', 'foo*']
> 
> for p in patterns:
>     if fnmatch.fnmatch(some_file_name, p):
>         return True
>
> ...is there a built-in function that will match using multiple patterns?

import re
pats = re.compile('|'.join(fnmatch.translate(p) for p in patterns))

if pats.match(some_file_name):
	return True

w.



More information about the Python-list mailing list