Files
troupe-standup/wizard-work/spark/quick_stats.py
T

49 lines
1.4 KiB
Python

#!/usr/bin/env python3
"""Quick file statistics: lines, words, bytes, and unique words."""
import sys
from pathlib import Path
from collections import Counter
def quick_stats(filepath: str) -> dict:
"""Return line count, word count, byte size, and top 5 most common words."""
path = Path(filepath)
if not path.exists():
return {"error": f"File not found: {filepath}"}
content = path.read_text()
lines = content.splitlines()
words = content.split()
word_counts = Counter(w.lower() for w in words if w.isalnum())
top_words = word_counts.most_common(5)
return {
"path": filepath,
"bytes": len(path.read_bytes()),
"lines": len(lines),
"words": len(words),
"unique_words": len(word_counts),
"top_5_words": top_words
}
def main():
if len(sys.argv) < 2:
print("Usage: quick_stats.py <file1> [file2...]")
sys.exit(1)
for filepath in sys.argv[1:]:
stats = quick_stats(filepath)
if "error" in stats:
print(f"ERROR: {stats['error']}")
continue
print(f"\n=== {stats['path']} ===")
print(f"Bytes: {stats['bytes']:,}")
print(f"Lines: {stats['lines']:,}")
print(f"Words: {stats['words']:,}")
print(f"Unique: {stats['unique_words']:,}")
print(f"Top words: {stats['top_5_words']}")
if __name__ == "__main__":
main()