-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathwc.py
More file actions
59 lines (48 loc) · 1.45 KB
/
wc.py
File metadata and controls
59 lines (48 loc) · 1.45 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
import argparse
import os
parser = argparse.ArgumentParser(
prog="wc",
description="Display numbers of line, words, and bytes in each file"
)
parser.add_argument("-l", action="store_true", help="Number of lines")
parser.add_argument("-w", action="store_true" ,help="Number of words")
parser.add_argument("-c", action="store_true" ,help="Number of bytes")
parser.add_argument("paths", nargs="+", help="The file to search")
args = parser.parse_args()
totalLines = 0
totalWords = 0
totalBytes = 0
lines = args.l
words = args.w
bytes = args.c
for path in args.paths:
try:
with open(path, "r") as f:
content = f.read()
except Exception as err:
print(f"Error reading file '{path}': {err}")
continue
lineCount = len(content.split("/n"))
wordCount = len(content.split())
byteCount = os.path.getsize(path)
totalLines += lineCount
totalWords += wordCount
totalBytes += byteCount
if lines:
print(f"\t{lineCount} {path}")
lineCount += 1
elif words:
print(f"\t{wordCount} {path}")
elif bytes:
print(f"\t{byteCount} {path}")
else:
print(f"\t{lineCount} \t{wordCount} \t{byteCount} {path}")
if len(args.paths) > 1:
if lines:
print(f"{totalLines} total")
elif words:
print(f"{totalWords} total")
elif bytes:
print(f"{totalBytes} total")
else:
print(f"\t{totalLines} \t{totalWords} \t{totalBytes} total")