25 lines
767 B
Python
25 lines
767 B
Python
#!/usr/bin/env python3
|
|
"""Quick file statistics utility - counts lines, words, characters."""
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
def file_stats(filepath):
|
|
"""Return (lines, words, chars) for a file."""
|
|
content = Path(filepath).read_text()
|
|
lines = content.count('\n')
|
|
words = len(content.split())
|
|
chars = len(content)
|
|
return lines, words, chars
|
|
|
|
if __name__ == '__main__':
|
|
if len(sys.argv) < 2:
|
|
print("Usage: file_stats.py <file> [file...]")
|
|
sys.exit(1)
|
|
|
|
for filepath in sys.argv[1:]:
|
|
try:
|
|
l, w, c = file_stats(filepath)
|
|
print(f"{filepath}: {l} lines, {w} words, {c} chars")
|
|
except FileNotFoundError:
|
|
print(f"ERROR: {filepath} not found")
|
|
sys.exit(2) |