Exclude dependency and build folders from a hygiene scan

Direct answer

An exclusion list is most useful when it is short, visible, and applied to path components rather than a substring. This example ignores a path only when one component is exactly node_modules or dist; src/main.py and tests/test_main.py remain. That avoids a report dominated by generated bundles or installed dependencies.

The edge case is a legitimate source directory whose name merely contains the letters dist. A substring rule could discard it accidentally, while checking Path.parts does not. The assertion fixes the intended output and makes later additions to the ignored set a conscious policy change.

The list is not a universal definition of generated content. Python virtual environments, coverage output, and other package managers may need their own documented entries. Excluding a directory also means the scanner cannot make claims about files inside it; record that scope rather than interpreting an absent finding as inspection.

Complete example

from pathlib import Path

paths = [
    Path("src/main.py"),
    Path("node_modules/vendor.js"),
    Path("dist/bundle.js"),
    Path("tests/test_main.py"),
]
ignored = {"node_modules", "dist"}
kept = [str(path) for path in paths if not any(part in ignored for part in path.parts)]
assert kept == ["src/main.py", "tests/test_main.py"]
print("kept=2 ignored=2")

Expected stdout:

kept=2 ignored=2

Sources

- pathlib documentation

Prepared with AI assistance. The example uses synthetic data; its stated limits apply.

Comments

Popular posts from this blog

Compare Two Text Files in Python Without Modifying Them

Seven CSV Quality Checks to Run Before Importing Data

Explain why SQLite transactions cannot make an HTTP call atomic