Lightweight recursive glob helpers for Python. Find files recursively, count lines, and sum sizes with simple functions.
Python 3 users should install the latest release. For Python 2, the final supported version is 1.4 (links below).
—
pip install rglobimport rglob
# List all Python files under a base directory
files = rglob.rglob("/path/to/project", "*.py")
# Same, starting from the current working directory
files_cwd = rglob.rglob_("*.py")
# Count non-empty, non-comment lines across matching files
non_empty_non_comment = rglob.lcount(
"/path/to/project",
"*.py",
lambda line: bool(line.strip()) and not line.lstrip().startswith("#"),
)
# Total size of all JPGs in megabytes (use provided unit helpers)
total_mb = rglob.tsize("/path/to/photos", "*.jpg", rglob.megabytes)Note: When invoking from a shell, quote or escape glob patterns so your shell doesn’t expand them before Python runs, e.g. "*.py".
rglob.rglob(base: str, pattern: str) -> list[str]: Recursively returns a list of paths matchingpatternunderbase.rglob.rglob_(pattern: str) -> list[str]: Same asrglob, using the current working directory asbase.rglob.lcount(base: str, pattern: str, func: Callable[[str], bool] = lambda _: True) -> int: Counts lines across all matching files, applyingfuncas a per-line predicate.rglob.tsize(base: str, pattern: str, func: Callable[[float], float] = rglob.megabytes) -> float: Sums sizes (in bytes) of matching files, then converts usingfunc.- Unit helpers:
rglob.kilobytes,rglob.megabytes,rglob.gigabytes,rglob.terabytes.
rglob can also be used as a command-line tool.
Important: quote your patterns
- Always quote or escape glob patterns so your shell does not expand them before Python runs.
- Wrong:
python3 -m rglob.cli find *.py(the shell expands*.pyfirst) - Right:
python3 -m rglob.cli find "*.py" - Installed entry point works the same:
rglob find "*.py"
rglob find "*.py"rglob lcount "*.py" --no-empty --no-commentsrglob tsize "*.py" --unit mb- Paths returned are not guaranteed to be sorted; call
sorted(...)if ordering matters. - Patterns use Python’s
globsyntax (e.g.,"*.py","**/*.py"is not supported here; recursion is handled byrglob). - Line counting opens files in text mode; ensure your files are decodable with the system default encoding.
- Python 3: Use the latest release (e.g., 1.7+ on PyPI).
- Python 2: Final supported release is 1.4.
Links:
- PyPI (Py3): https://pypi.org/project/rglob/1.7/
- PyPI (Py2): https://pypi.org/project/rglob/1.4/
git clone https://github.com/chris-piekarski/python-rglob.git
cd python-rglob
python -m venv .venv && source .venv/bin/activate
pip install -e .This repo includes BDD tests using behave.
pip install behave
behaveNote: The core library supports Python 3 (see setup.py).
rglob predates glob.glob(..., recursive=True) and offers a tiny, easy-to-read implementation with convenient helpers for line counting and size aggregation. It’s useful for quick scripts and small utilities where pulling in heavier tools is unnecessary.
Apache 2.0 — see LICENSE.