Python Notes (0.14.0)

5. glob module

Status:in progress

The glob module implements globbing of directory contents.

See also

os.listdir(), os

5.1. Quick start

>>> import glob
>>> glob.glob('/home/user', '*')

The glob.glob returns the list of files with their full path (unlike os.listdir()) and is more powerful than os.listdir that does not use wildcards.

In addition, glob contains the os, sys and re modules.

5.2. wildcards

Wildcard Matches Example
* any characters *.txt matches all files with the txt extension
? any one character ??? matches files with 3 characters long
[] any character listed in the brackets [ABC]* matches files starting with A,B or C
[..] any character in the range listed in brackets [A..Z]* matches files starting with capital letters
[!] any character listed in the brackets [!ABC]* matches files that do not start with A,B or C

5.3. methods

glob.glob1() is equivalent to glob.glob(). Instead of providing a pathname, you decompose it into a dirname and a pattern. So:



glob.glob1('/home/user/', '*')


glob.glob0             glob.magic_check