all Python 3globterm
glob
module
Finding paths by a shell pattern: every *.txt in a directory, or recursively through ** in nested folders.
Syntax
import glob
glob returns a list of paths matching a pattern as strings; the object-oriented version is Path.glob from pathlib, and matching a name without touching the disk is fnmatch. The result order is unspecified, so sort it. Recursion with ** works only with recursive=True, and files starting with a dot are not found by default.
Examples
import glob
from pathlib import Path
for name in ['a.txt', 'b.txt', 'c.py']:
Path(name).write_text('')
print(sorted(glob.glob('*.txt'))) # → ['a.txt', 'b.txt']
See also
Open in the full glossarydocs.python.orgReport an inaccuracy