The glob module

This module generates lists of files matching given patterns, just like the Unix shell.

File patterns are similar to regular expressions, but simpler. An asterisk (*) matches zero or more characters, and a question mark (?) exactly one character. You can also use brackets to indicate character ranges, such as [0-9] for a single digit. All other characters match themselves.

glob(pattern) returns a list of all files matching a given pattern.

Example: Using the glob module
# File: glob-example-1.py

import glob

for file in glob.glob("samples/*.jpg"):
    print file

samples/sample.jpg

Note that glob returns full path names, unlike the os.listdir function. glob uses the fnmatch module to do the actual pattern matching.

 

[comment on/vote for this article]

A Django site. this page was rendered by a django application in 0.02s 2008-09-08 17:46:35.980750. hosted by webfaction.