-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathwc.py
More file actions
177 lines (140 loc) · 5.44 KB
/
wc.py
File metadata and controls
177 lines (140 loc) · 5.44 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# import { program } from "commander";
# import { promises as fs } from "node:fs";
# import process from "node:process";
# import { stat } from "node:fs/promises";
# program
# .name("count-containing-lines-words-characters")
# .description("Counts lines, words or characters in a file (or all files) inside a directory")
# .option("-l, --line", "The number of lines in each file")
# .option("-w, --word", "The number of words in each file")
# .option("-c, --character", "The number of characters in each file")
# .argument("<path...>", "The file path to process");
# program.parse();
# const argv = program.args;
# const options = program.opts();
# function counter(item) {
# const lines = item.trim().split("\n").length;
# const words = item.split(/\s+/).filter(Boolean).length;
# const characters = item.length;
# return { lines, words, characters };
# }
# let totalLines = 0;
# let totalWords = 0;
# let totalCharacters = 0;
# let fileCount = 0;
# for (const path of argv) {
# const pathInfo = await stat(path);
# if (pathInfo.isFile()) {
# const content = await fs.readFile(path, "utf-8");
# const stats = counter(content);
# if (options.line) {
# console.log(`${stats.lines} ${path}`);
# } else if (options.word) {
# console.log(`${stats.words} ${path}`);
# } else if (options.character) {
# console.log(`${stats.characters} ${path}`);
# } else {
# console.log(`${stats.lines} ${stats.words} ${stats.characters} ${path}`);
# }
# totalLines += stats.lines;
# totalWords += stats.words;
# totalCharacters += stats.characters;
# fileCount++;
# } else if (pathInfo.isDirectory()) {
# const files = await fs.readdir(path);
# for (const file of files) {
# const filePath = `${path}/${file}`;
# const fileContent = await fs.readFile(filePath, "utf-8");
# const stats = counter(fileContent);
# if (options.line) {
# console.log(`${stats.lines} ${filePath}`);
# } else if (options.word) {
# console.log(`${stats.words} ${filePath}`);
# } else if (options.character) {
# console.log(`${stats.characters} ${filePath}`);
# } else {
# console.log(`${stats.lines} ${stats.words} ${stats.characters} ${filePath}`);
# }
# totalLines += stats.lines;
# totalWords += stats.words;
# totalCharacters += stats.characters;
# fileCount++;
# }
# }
# }
# if (fileCount > 1) {
# if (options.line) {
# console.log(`${totalLines} total`);
# } else if (options.word) {
# console.log(`${totalWords} total`);
# } else if (options.character) {
# console.log(`${totalCharacters} total`);
# } else {
# console.log(`${totalLines} ${totalWords} ${totalCharacters} total`);
# }
# }
import argparse
import os
parser = argparse.ArgumentParser(
prog="counter",
description="Counts lines, words or characters in a file (or all files) inside a directory",
)
parser.add_argument("-l", "--line", dest="line", help="The number of lines in each file", action="store_true")
parser.add_argument("-w", "--word", dest="word", help="The number of words in each file", action="store_true")
parser.add_argument("-c", "--char", dest="char", help="The number of characters in each file", action="store_true")
parser.add_argument("paths", help="The file(s)/path(s) to read from", nargs="+")
args = parser.parse_args()
def counter(item):
lines = len(item.strip().split("\n"))
words = len(item.split())
characters = len(item)
return {"lines": lines, "words": words, "characters": characters}
total_lines = 0
total_words = 0
total_characters = 0
file_count = 0
for path in args.paths:
if os.path.isfile(path):
with open(path, "r") as f:
content = f.read()
stats = counter(content)
if args.line:
print(f"{stats['lines']} {path}")
elif args.word:
print(f"{stats['words']} {path}")
elif args.char:
print(f"{stats['characters']} {path}")
else:
print(f"{stats['lines']} {stats['words']} {stats['characters']} {path}")
total_lines += stats['lines']
total_words += stats['words']
total_characters += stats['characters']
file_count += 1
elif os.path.isdir(path):
for file in os.listdir(path):
file_path = os.path.join(path, file)
if os.path.isfile(file_path):
with open(file_path, "r") as f:
content = f.read()
stats = counter(content)
if args.line:
print(f"{stats['lines']} {file_path}")
elif args.word:
print(f"{stats['words']} {file_path}")
elif args.char:
print(f"{stats['characters']} {file_path}")
else:
print(f"{stats['lines']} {stats['words']} {stats['characters']} {file_path}")
total_lines += stats['lines']
total_words += stats['words']
total_characters += stats['characters']
file_count += 1
if file_count > 1:
if args.line:
print(f"{total_lines} total")
elif args.word:
print(f"{total_words} total")
elif args.char:
print(f"{total_characters} total")
else:
print(f"{total_lines} {total_words} {total_characters} total")