More Efficient fnmatch.fnmatch for multiple patterns?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • abcd

    More Efficient fnmatch.fnmatch for multiple patterns?

    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?

  • Brian Beck

    #2
    Re: More Efficient fnmatch.fnmatch for multiple patterns?

    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

    Comment

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

      #3
      Re: More Efficient fnmatch.fnmatch for multiple patterns?

      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.tr anslate(p) for p in patterns))

      if pats.match(some _file_name):
      return True

      w.

      Comment

      • Gabriel Genellina

        #4
        Re: More Efficient fnmatch.fnmatch for multiple patterns?

        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(fil ename, 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!


        Comment

        • Gabriel Genellina

          #5
          Re: More Efficient fnmatch.fnmatch for multiple patterns?

          At Monday 8/1/2007 15:55, Wojciech =?ISO-8859-2?Q?Mu=B3a?= wrote:
          >pats = re.compile('|'. join(fnmatch.tr anslate(p) for p in patterns))
          Hmm, fnmatch.transla te 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!


          Comment

          Working...