The glob module finds filesystem pathnames that match shell-style wildcard patterns. Its glob function returns matching files from the current working directory, making it useful when filenames follow a predictable pattern but are not known exactly. section 12.4
Use these pattern forms to control which pathnames match:
- * matches any sequence of characters. section 12.4
- ? matches any single character. section 12.4
- [h,H] or [0-9] matches one character from the listed sequence or range. section 12.4
Match files in the current directory
import glob glob.glob("*") # all matching names glob.glob("*json") # names ending in json glob.glob("?.tmp") # one-character names before .tmp glob.glob("[0-9].tmp") # digit names before .tmp section 12.4
import glob
glob.glob("*")
['anscombe.json',
'README.md',
'california_housing_train.csv',
'california_housing_test.csv',
'mnist_test.csv',
'mnist_train_small.csv']
glob.glob("*json")
['anscombe.json']
glob.glob("?.tmp")
['1.tmp', '2.tmp', 'a.tmp']
glob.glob("[0-9].tmp")
['1.tmp', '2.tmp']
Inspect the resulting pathname lists for broad, suffix-based, single-character, and character-range patterns. section 12.4
For larger file-wrangling tasks, pathlib also provides a path object's glob method. That alternative can yield Path objects and fit naturally with operations such as renaming matching files and moving them into an archive. section 20.2


