-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathwc.py
More file actions
53 lines (44 loc) · 1.47 KB
/
Copy pathwc.py
File metadata and controls
53 lines (44 loc) · 1.47 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
import sys
from pathlib import Path
def calculateCounts(inputFiles):
return {
"lines": len(inputFiles.split("\n")) - 1,
"words": len(inputFiles.split()),
"bytes": len(inputFiles),
}
# * `wc -l sample-files/3.txt`
# * `wc -l sample-files/*`
def countLines(listOfFiles):
for file in listOfFiles:
content = Path(file).read_text(encoding="utf-8")
counts = calculateCounts(content)
print(f"{counts['lines']} {file}")
# * `wc -w sample-files/3.txt`
def countWords(listOfFiles):
for file in listOfFiles:
content = Path(file).read_text(encoding="utf-8")
# const wordsCounted = content.split(" ").filter(word => word !== "").length;
counts = calculateCounts(content)
print(f"{counts['words']} {file}")
# * `wc -c sample-files/3.txt`
def countBytes(listOfFiles):
for file in listOfFiles:
content = Path(file).read_text(encoding="utf-8")
counts = calculateCounts(content)
print(f"{counts['bytes']} {file}")
# * `wc sample-files/*`
def countAll(listOfFiles):
for file in listOfFiles:
content = Path(file).read_text(encoding="utf-8")
counts = calculateCounts(content)
print(f"{counts['lines']} {counts['words']} {counts['bytes']} {file}")
argv = sys.argv[1:]
files = [arg for arg in argv if not arg.startswith("-")]
if "-l" in argv:
countLines(files)
elif "-w" in argv:
countWords(files)
elif "-c" in argv:
countBytes(files)
else:
countAll(files)