Counts total lines across every file matching a glob pattern under the current directory. A quick, dependency-free stand-in for "how big is this codebase/log set".
#!/usr/bin/env python3
"""Count total lines across files matching a glob pattern (relative to cwd)."""
import sys
from pathlib import Path
if __name__ == "__main__":
if len(sys.argv) != 2:
sys.exit("usage: count-lines.py '<glob-pattern>' (e.g. 'src/**/*.py')")
total = 0
for p in Path(".").glob(sys.argv[1]):
if p.is_file():
with open(p, "rb") as f:
total += sum(1 for _ in f)
print(total)
| pattern | string — positional argument, a glob pattern relative to the current directory, e.g. src/**/*.py (quote it so the shell doesn't expand it first) |
|---|
0 if nothing matches.
$ python3 count-lines.py 'src/**/*.py'
4218
pathlib.Path.glob, not shell expansion, so unquoted-glob shell injection isn't a concern.