From 5d619049c85002a8c501c296a0690e464292694b Mon Sep 17 00:00:00 2001 From: spark Date: Sat, 24 Oct 2026 13:29:34 +0000 Subject: [PATCH] spark: add text_stats.py utility for counting lines/words/chars in text files --- wizard-work/spark/text_stats.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 wizard-work/spark/text_stats.py diff --git a/wizard-work/spark/text_stats.py b/wizard-work/spark/text_stats.py new file mode 100644 index 0000000..d782ebd --- /dev/null +++ b/wizard-work/spark/text_stats.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 +""" +text_stats.py - Quick utility to count lines, words, and characters in text files. +Usage: python text_stats.py [file2] ... +""" +import sys + +def analyze_text(filepath): + """Return (lines, words, chars) for a text file.""" + try: + with open(filepath, 'r', encoding='utf-8', errors='replace') as f: + content = f.read() + lines = len(content.splitlines()) + words = len(content.split()) + chars = len(content) + return lines, words, chars + except Exception as e: + return None, None, None + +def main(): + if len(sys.argv) < 2: + print("Usage: python text_stats.py [file2] ...") + sys.exit(1) + + for filepath in sys.argv[1:]: + lines, words, chars = analyze_text(filepath) + if lines is None: + print(f"{filepath}: error reading file") + continue + print(f"{filepath}: {lines} lines, {words} words, {chars} chars") + +if __name__ == '__main__': + main() \ No newline at end of file