-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathwc.py
More file actions
54 lines (43 loc) · 1.75 KB
/
wc.py
File metadata and controls
54 lines (43 loc) · 1.75 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
import sys
import os
# Flag Detection
show_lines = "-l" in sys.argv
show_words = "-w" in sys.argv
show_chars = "-c" in sys.argv
# If no flags then show all.
show_all = not (show_lines or show_words or show_chars)
# Filter files excluding flags
files = [arg for arg in sys.argv[1:] if not arg.startswith("-")]
# Total counters
total_l, total_w, total_c = 0, 0, 0
for filename in files:
try:
with open(filename, 'r') as f:
lines = f.readlines()
# files calculation
l_count = len(lines)
w_count = sum(len(line.split()) for line in lines)
# os.path.getsize for bytes, in .js I used const bytes = Buffer.byteLength(content);
c_count = os.path.getsize(filename)
# Update totals
total_l += l_count
total_w += w_count
total_c += c_count
# Printing Logic
output = []
if show_lines or show_all: output.append(f"{l_count:>8}")
if show_words or show_all: output.append(f"{w_count:>8}")
if show_chars or show_all: output.append(f"{c_count:>8}")
print(f"{''.join(output)} {filename}")
except FileNotFoundError:
print(f"wc: {filename}: No such file or directory")
except IsADirectoryError:
print(f"wc: {filename}: Is a directory")
print(f"{'0':>8} {'0':>8} {'0':>8} {filename}")
# Print Total if there were multiple files
if len(files) > 1:
output_total = []
if show_lines or show_all: output_total.append(f"{total_l:>8}")
if show_words or show_all: output_total.append(f"{total_w:>8}")
if show_chars or show_all: output_total.append(f"{total_c:>8}")
print(f"{''.join(output_total)} total")