26 lines
780 B
Python
26 lines
780 B
Python
#!/usr/bin/env python3
|
|
"""Lines, Words, Characters counter - A simple text file analysis tool."""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def count_file(path: str) -> dict:
|
|
"""Return lines, words, and characters in a file."""
|
|
text = Path(path).read_text()
|
|
lines = text.count('\n') + (1 if text and not text.endswith('\n') else 0)
|
|
if not text:
|
|
lines = 0
|
|
words = len(text.split())
|
|
chars = len(text)
|
|
return {"lines": lines, "words": words, "chars": chars}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 2:
|
|
print("Usage: lwc_counter.py <file>", file=sys.stderr)
|
|
sys.exit(1)
|
|
result = count_file(sys.argv[1])
|
|
print(f"Lines: {result['lines']}")
|
|
print(f"Words: {result['words']}")
|
|
print(f"Chars: {result['chars']}") |