-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwc.py
More file actions
92 lines (75 loc) · 2.71 KB
/
wc.py
File metadata and controls
92 lines (75 loc) · 2.71 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import argparse
import os
import sys
from tabulate import tabulate
def parse_args():
parser = argparse.ArgumentParser(
description="word, line and byte count",
)
parser.add_argument("paths", nargs="+",
help="The file path(s) to process.")
parser.add_argument(
"-l",
"--lines",
action="store_true",
help="The number of lines in each input file is written to the standard output.",
)
parser.add_argument(
"-w",
"--words",
action="store_true",
help="The number of words in each input file is written to the standard output.",
)
parser.add_argument(
"-c",
"--bytes",
action="store_true",
dest="bytes",
help="The number of bytes in each input file is written to the standard output.",
)
return parser.parse_args()
def main():
args = parse_args()
try:
file_paths: list[str] = args.paths
results: dict[str, dict[str, int]] = {}
for file_path in file_paths:
stats = os.stat(file_path)
count = {"lines": 0, "words": 0, "bytes": stats.st_size}
with open(file_path, "r", encoding="utf-8") as file:
for line in file:
count["lines"] += 1
trimmed = line.strip()
if len(trimmed) > 0:
count["words"] += len(trimmed.split())
results[file_path] = count
if len(file_paths) > 1:
total = {"lines": 0, "words": 0, "bytes": 0}
for file_count in results.values():
total["lines"] += file_count["lines"]
total["words"] += file_count["words"]
total["bytes"] += file_count["bytes"]
results["total"] = total
no_options_provided = not (args.lines or args.words or args.bytes)
selected_option_keys: list[str] = []
if args.lines:
selected_option_keys.append("lines")
if args.words:
selected_option_keys.append("words")
if args.bytes:
selected_option_keys.append("bytes")
output_columns = [
"lines", "words", "bytes"] if no_options_provided else selected_option_keys
rows: list[list[str | int]] = []
for name, values in results.items():
rows.append([name] + [values[column] for column in output_columns])
if no_options_provided:
print(tabulate(rows, headers=[
"index"] + output_columns))
else:
print(tabulate(rows, headers=[
"index"] + output_columns))
except OSError as err:
print(str(err), file=sys.stderr)
return 0
main()