Globbing

glob module

  • Returns filenames in a directory that match a pattern
     import glob
     a = glob.glob("*.html")
     b = glob.glob("image[0-5]*.gif")
  • Pattern matching is performed using rules of Unix shell.
  • Tilde (~) and variable expansion is not performed.

fnmatch module

  • Matches filenames according to rules of Unix shell
     import fnmatch
     if fnmatch(filename,"*.html"):
         ...
  • Case-sensitivity depends on the operating system.
<<< O'Reilly OSCON 2000, Advanced Python Programming, Slide 38
July 17, 2000, beazley@cs.uchicago.edu
>>>