2026년 10월 18일 일요일

Find files recursively with Path.rglob() while excluding hidden paths

Suppose a script needs every .csv file under data, except files whose names or parent directories begin with a dot. In Python 3.14, Path.rglob() finds candidates throughout that tree. A filter then decides which candidates belong in the result. If the script must skip hidden directories during the search itself, use Path.walk() to prune them.

Find files recursively with Path.rglob() while excluding hidden paths
A reading map of the checks explained below.

Filter every component below the root

Here is a complete collection step: from pathlib import Path root = Path("data") files = sorted( path for path in root.rglob("*.csv") if path.is_file() and not any(part.startswith(".") for part in path.relative_to(root).parts) ) The relative_to(root).parts expression examines only the portion beneath data. It rejects data/.cache/report.csv and data/reports/.draft.csv as well as data/.summary.csv. It does not reject every descendant merely because the chosen root itself has a dot-prefixed name. The is_file() check limits the collection to files; by default, it also accepts a symlink whose target is a regular file. See Python 3.14’s Path.rglob() and Path.is_file() documentation: https://docs.python.org/3.14/library/pathlib.html#pathlib.Path.rglob and https://docs.python.org/3.14/library/pathlib.html#pathlib.Path.is_file.

Path.rglob("*.csv") searches at the root and below it, and its results retain the root prefix. The sorted() call gives the script a stable presentation order for the results it receives; the search itself supplies no particular order. See https://docs.python.org/3.14/library/pathlib.html#pathlib.Path.rglob.

Sources: pathlib — Object-oriented filesystem paths

Prune hidden directories before entering them

The filter above still lets the recursive search visit a hidden directory before discarding matches found there. For a search that skips those directories during descent, walk from the same root and edit the directory list: from pathlib import Path root = Path("data") files = [] for directory, dirnames, filenames in root.walk(): dirnames[:] = [name for name in dirnames if not name.startswith(".")] files.extend( directory / name for name in filenames if name.endswith(".csv") and not name.startswith(".") and (directory / name).is_file() ) files.sort() Editing dirnames in place during the default top-down walk prevents Path.walk() from descending into removed directories. The filename check handles hidden files in directories that remain. This example uses a literal .csv ending rather than a glob pattern; if the intended rule is more complex, define it explicitly where filenames are selected. See https://docs.python.org/3.14/library/pathlib.html#pathlib.Path.walk.

Path.walk() does not follow directory symlinks by default. With that default, a link to a directory is placed among filenames, and the is_file() check above leaves it out. If the search needs a different link policy, decide that separately from the hidden-name policy. See https://docs.python.org/3.14/library/pathlib.html#pathlib.Path.walk.

Sources: pathlib — Object-oriented filesystem paths

Choose the exclusion rule deliberately

A hidden path here means a component whose name starts with a dot. A directory called build is visible under that rule, even if the script should ignore it. Add such names to the dirnames filter when using Path.walk(), or add a separate component check when filtering rglob() results. A name like report.v2.csv is also visible because its dot is not at the start.

The examples treat data as the search boundary and allow the caller to choose even a dot-prefixed root. That choice avoids making a relative name test depend on parent directories outside the requested search. If the caller instead wants to reject a hidden root, check root.name before starting. These are application rules, so writing them beside the search makes later changes easier to review.

Sources: pathlib — Object-oriented filesystem paths

Know what a result list establishes

Pathlib wildcards can match dot-prefixed names, and recursive double-star expansion does not follow directory symlinks by default. The recurse_symlinks option changes that traversal choice. See the comparison with glob at https://docs.python.org/3.14/library/pathlib.html#comparison-to-the-glob-module and Path.rglob() at https://docs.python.org/3.14/library/pathlib.html#pathlib.Path.rglob.

Path.rglob() suppresses filesystem scanning errors, including permission errors. Path.walk() also ignores scanning errors by default, but its on_error argument can receive them. A short result list therefore does not establish that every directory was readable. Use on_error when the script needs to detect an incomplete walk. See https://docs.python.org/3.14/library/pathlib.html#pathlib.Path.walk.

Sources: pathlib — Object-oriented filesystem paths

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

댓글 없음:

댓글 쓰기