By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,986 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,986 IT Pros & Developers. It's quick & easy.

More Efficient fnmatch.fnmatch for multiple patterns?

P: n/a
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?

Jan 8 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
I don't see anything in the fnmatch and glob modules... but I didn't look
very hard because what the heck is wrong with the four line solution you
have? Looks fine to me.

--
Brian Beck
Adventurer of the First Order

Jan 8 '07 #2

P: n/a
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.
Jan 8 '07 #3

P: n/a
At Monday 8/1/2007 15:10, 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?
matched = any(fnmatch(filename, p) for p in patterns)
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Jan 8 '07 #4

P: n/a
At Monday 8/1/2007 15:55, Wojciech =?ISO-8859-2?Q?Mu=B3a?= wrote:
>pats = re.compile('|'.join(fnmatch.translate(p) for p in patterns))
Hmm, fnmatch.translate does not appear in the docs.
But it does on the module docstring, and in __all__, so certainly
it's supposed to be public.
I'll file a bug report.
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Jan 8 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.