-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathwc.py
More file actions
64 lines (49 loc) · 1.48 KB
/
wc.py
File metadata and controls
64 lines (49 loc) · 1.48 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
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("files", nargs="+")
parser.add_argument("-l", action="store_true", help="show line count")
parser.add_argument("-w", action="store_true", help="show word count")
parser.add_argument("-c", action="store_true", help="show byte count")
args = parser.parse_args()
show_lines = args.l
show_words = args.w
show_bytes = args.c
if not show_lines and not show_words and not show_bytes:
show_lines = True
show_words = True
show_bytes = True
total_lines = 0
total_words = 0
total_bytes = 0
for file in args.files:
try:
f = open(file, "rb")
content = f.read()
f.close()
line_count = content.count(b"\n")
word_count = len(content.split())
byte_count = len(content)
total_lines += line_count
total_words += word_count
total_bytes += byte_count
output = []
if show_lines:
output.append(str(line_count))
if show_words:
output.append(str(word_count))
if show_bytes:
output.append(str(byte_count))
output.append(file)
print(" ".join(output))
except OSError:
print("wc:", file, "not found")
if len(args.files) > 1:
output = []
if show_lines:
output.append(str(total_lines))
if show_words:
output.append(str(total_words))
if show_bytes:
output.append(str(total_bytes))
output.append("total")
print(" ".join(output))