2026년 10월 18일 일요일

Choosing Path.glob(), Path.rglob(), or glob.glob() for file searches

For Python 3.14, choose a search API by the shape of its results as well as its pattern. A script that works with Path objects may favor pathlib; a caller that needs strings relative to a specified search root may favor glob.glob(). The table gives the practical differences before two short examples.

Choosing Path.glob(), Path.rglob(), or glob.glob() for file searches
A reading map of the checks explained below.

Compare the three APIs

Feature | Path.glob() | Path.rglob() | glob.glob() Recursion | Use ** in the pattern | Searches recursively by adding **/ before the pattern | Use ** with recursive=True Hidden components | Wildcards may match dot-prefixed names | Same | Wildcards exclude them by default; include_hidden=True permits them Directory symlinks | ** does not descend through them by default | Same | Recursive ** may traverse them Return type | Iterator of Path objects | Iterator of Path objects | List of strings Root-relative results | Results retain the Path root prefix; use relative_to(root) | Same | With relative pathname and root_dir, results are relative to root_dir

The rows describe Python 3.14’s documented behavior. In particular, a Path.glob() pattern containing ** is recursive, so the method is not inherently limited to one level. Path.rglob() saves you from writing that leading recursive component. For glob.glob(), recursive=True gives ** its recursive meaning; include_hidden=True changes what wildcards may match. Sources: https://docs.python.org/3.14/library/pathlib.html#pathlib.Path.glob, https://docs.python.org/3.14/library/pathlib.html#pathlib.Path.rglob, https://docs.python.org/3.14/library/pathlib.html#comparison-to-the-glob-module, and https://docs.python.org/3.14/library/glob.html#glob.glob.

Sources: pathlib — Object-oriented filesystem paths, glob — Unix style pathname pattern expansion

Choose pathlib for Path objects

Suppose a script checks the immediate contents of assets for PNG files, then separately needs PNG files anywhere below assets: from pathlib import Path root = Path("assets") top_level = list(root.glob("*.png")) any_depth = list(root.rglob("*.png")) Both collections contain Path objects with assets in their displayed paths. If the caller needs names relative to assets, convert each result with path.relative_to(root). The first pattern addresses one level; the second also searches descendants. The equivalent recursive Path.glob() pattern is "**/*.png". See https://docs.python.org/3.14/library/pathlib.html#pathlib.Path.glob and https://docs.python.org/3.14/library/pathlib.html#pathlib.Path.rglob.

Neither pathlib call treats a leading dot as a reason to exclude a matching name. If the intended search should omit files inside .cache or a similarly named directory, apply a rule to the components relative to root, or prune directories with Path.walk(). That policy belongs to the task rather than to the choice between Path.glob() and Path.rglob(). See https://docs.python.org/3.14/library/pathlib.html#comparison-to-the-glob-module.

Sources: pathlib — Object-oriented filesystem paths

Choose glob.glob() for root-relative strings

Suppose a manifest expects strings such as "chapter/notes.txt" relative to a content directory: import glob entries = glob.glob("**/*.txt", root_dir="content", recursive=True) Here root_dir sets the search location, while the relative pathname keeps that root out of each returned string. Leave include_hidden at its default when wildcard matches should omit dot-prefixed components; set include_hidden=True when they should be eligible. The function returns a list, so sort it if the manifest needs a defined order. See https://docs.python.org/3.14/library/glob.html#glob.glob.

A leading directory written in the pathname remains in returned strings when root_dir is not used. For example, glob.glob("content/*.txt") returns strings beginning with content/ for matches to that pattern. Supplying root_dir with a relative pathname is the direct way to express the desired output boundary. See the examples and root_dir description at https://docs.python.org/3.14/library/glob.html#glob.glob.

Sources: glob — Unix style pathname pattern expansion

Check file type, links, and completeness separately

All three searches can yield matching paths that are not regular files. Check file type before treating a match as file content. Link behavior also deserves an explicit decision: pathlib’s recursive ** avoids following directory symlinks by default, while glob.glob() can traverse directory symlinks during recursive ** expansion. For pathlib, recurse_symlinks=True changes the default. See https://docs.python.org/3.14/library/pathlib.html#comparison-to-the-glob-module and https://docs.python.org/3.14/library/glob.html#glob.glob.

These methods can suppress filesystem scanning errors, including permission errors. Their output is a collection of discovered matches, not proof that every directory was inspected. The examples and table summarize documented Python 3.14 behavior; they are not benchmark results or observations from an executed search. See https://docs.python.org/3.14/library/pathlib.html#pathlib.Path.rglob and https://docs.python.org/3.14/library/glob.html#glob.glob.

Sources: pathlib — Object-oriented filesystem paths, glob — Unix style pathname pattern expansion

Primary sources checked: 2026-10-17. Prepared with AI assistance and a separate evidence review. This is documentation-based guidance, not a hands-on test or performance benchmark.

Read the companion guide

댓글 없음:

댓글 쓰기