More Efficient fnmatch.fnmatch for multiple patterns?

A

abcd

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?
 
B

Brian Beck

abcd said:
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.
 
G

Gabriel Genellina

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
 
W

Wojciech =?ISO-8859-2?Q?Mu=B3a?=

abcd said:
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.
 
G

Gabriel Genellina

At said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top