Repository navigation
Expand file tree
/
Copy pathwc.py
More file actions
47 lines (38 loc) · 1.42 KB
/
Copy pathwc.py
File metadata and controls
47 lines (38 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""Count lines, words, and bytes in files."""
import argparse
from pathlib import Path
import sys
def count_file(filename):
text = Path(filename).read_bytes().decode("utf-8")
return len(text.splitlines()), len(text.split()), len(text)
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("-l", action="store_true", help="print line counts")
parser.add_argument("-w", action="store_true", help="print word counts")
parser.add_argument("-c", action="store_true", help="print byte counts")
parser.add_argument("files", nargs="+")
args = parser.parse_args()
selected = [args.l, args.w, args.c]
if not any(selected):
selected = [True, True, True]
def print_counts(counts, label):
values = [str(value) for value, enabled in zip(counts, selected) if enabled]
print(" ".join([*values, label]))
totals = [0, 0, 0]
successful = 0
status = 0
for filename in args.files:
try:
counts = count_file(filename)
except (OSError, UnicodeError) as error:
print(f"wc: {filename}: {error}", file=sys.stderr)
status = 1
continue
print_counts(counts, filename)
totals = [total + value for total, value in zip(totals, counts)]
successful += 1
if successful > 1:
print_counts(totals, "total")
return status
if __name__ == "__main__":
sys.exit(main())