28 lines
707 B
Python
28 lines
707 B
Python
#!/usr/bin/env python3
|
|
"""Simple word/line/char counter for files."""
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
def count_file(path: str) -> dict:
|
|
"""Return line, word, and char counts for a file (wc-style)."""
|
|
content = Path(path).read_text()
|
|
# wc -l counts newline characters, not lines
|
|
return {
|
|
'lines': content.count('\n'),
|
|
'words': len(content.split()),
|
|
'chars': len(content)
|
|
}
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
print("Usage: wordcount.py <file>", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
result = count_file(sys.argv[1])
|
|
print(f"{result['lines']} {result['words']} {result['chars']} {sys.argv[1]}")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main() |