On this page
glob
Prev: gettext | Up: stdlib_module | Next: graphlib
Rel: pathlib, os, io, time, fileinput
Ref: docs.python.org — glob
Tags: #public #python3 #stdlib
Import
import glob
Overview
Unix-style filesystem wildcard expansion (glob, iglob, escape, translate) with root_dir and recursive=True (**) support. Prefer Path.glob() / Path.rglob() from pathlib when already working with Path objects; reach for glob.iglob() when you want lazy string paths or include_hidden=True.
Quick Start
import glob
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
(Path(tmp) / "a.py").write_text("", encoding="utf-8")
(Path(tmp) / "b.md").write_text("", encoding="utf-8")
matches = glob.glob("*.py", root_dir=tmp)
assert matches == ["a.py"]
Implementation Walkthrough
1. Lazy Recursive Search with iglob() & root_dir
iglob() yields paths lazily without materializing the entire list in memory, and root_dir avoids calling os.chdir():
import glob
def iter_markdown(root: str) -> list[str]:
return sorted(glob.iglob("**/*.md", root_dir=root, recursive=True))
2. Escaping Literal Brackets in Directory Names (glob.escape)
If a base path might contain [ or ] (common in project folders or Next.js routes), escape it before appending a wildcard:
import glob
safe_pattern = glob.escape("/tmp/data[2026]") + "/*.json"
assert safe_pattern == "/tmp/data[[]2026]/*.json"
Gotchas & Boundaries
glob()results are returned in arbitrary OS directory order: Always wrap withsorted(...)when deterministic ordering matters.- Hidden dotfiles (
.env,.git) are skipped by default: Passinclude_hidden=True(Python 3.11+) when you intentionally want to match them. **requiresrecursive=True: Withoutrecursive=True,**/*.pyonly matches one directory level deep.
Common Functions
- glob.escape() —
escape(pathname)— Escape all special characters. - glob.glob() —
glob(pathname, *, root_dir=None, dir_fd=None, recursive=False, include_hidden=False)— Return a list of paths matching apathnamepattern. - glob.iglob() —
iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False, include_hidden=False)— Return an iterator which yields the paths matching apathnamepattern. - glob.translate() —
translate(pat, *, recursive=False, include_hidden=False, seps=None)— Translate a pathname with shell wildcards to a regular expression.
Linked from
Related notes
- tuts/python3/stdlib_module/gettext Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/graphlib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/fileinput Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/fnmatch links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/glob/escape Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/glob/glob Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/glob/iglob Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/glob/translate Linked here; shared tags: #stdlib