More Efficient fnmatch.fnmatch for multiple patterns?

Discussion in 'Python' started by abcd, Jan 8, 2007.

  1. abcd

    abcd Guest

    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?
    abcd, Jan 8, 2007
    #1
    1. Advertising

  2. abcd

    Brian Beck Guest

    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
    Brian Beck, Jan 8, 2007
    #2
    1. Advertising

  3. 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
    Gabriel Genellina, Jan 8, 2007
    #3
  4. 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.
    Wojciech =?ISO-8859-2?Q?Mu=B3a?=, Jan 8, 2007
    #4
  5. 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
    Gabriel Genellina, Jan 8, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. crichmon
    Replies:
    4
    Views:
    471
    Mabden
    Jul 7, 2004
  2. Oliver Wong
    Replies:
    8
    Views:
    634
    Oliver Wong
    Jul 3, 2006
  3. kepioo
    Replies:
    2
    Views:
    380
    kepioo
    Apr 9, 2006
  4. Replies:
    2
    Views:
    343
    Fredrik Lundh
    Jun 5, 2006
  5. Graeme Downes

    Regular Expression for fnmatch function

    Graeme Downes, Jan 14, 2005, in forum: C Programming
    Replies:
    1
    Views:
    433
    Keith Thompson
    Jan 14, 2005
Loading...

Share This Page