Skip to content

Commit 11e8232

Browse files
committed
working on wc flags
1 parent ded994c commit 11e8232

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import argparse
2+
import re
3+
4+
def format_number(number_array):
5+
return [str(number).rjust(4) for number in number_array]
6+
7+
parser = argparse.ArgumentParser(
8+
prog="custom wc in python",
9+
description="trying to match behaviour in the actual wc"
10+
)
11+
12+
parser.add_argument("-l", "--lines", action="store_true", help="count how many lines")
13+
parser.add_argument("-w", "--words", action="store_true", help="count how many words")
14+
parser.add_argument("-c", "--characters", action="store_true", help="count how many characters")
15+
parser.add_argument("path", nargs="+", help="path to process")
16+
17+
args = parser.parse_args()
18+
19+
files_array = args.path
20+
21+
total_lines = 0
22+
total_words = 0
23+
total_characters = 0
24+
25+
numbers_row_array = []
26+
27+
for file_path in files_array:
28+
with open(file_path) as file:
29+
context = file.read()
30+
lines = len(context.split("\n")) - 1
31+
words = len(re.findall(r"\S+", context))
32+
characters = len(context)
33+
34+
if (args.lines):
35+
numbers_row_array.append(lines)
36+
if (args.words):
37+
numbers_row_array.append(words)
38+
if (args.characters):
39+
numbers_row_array.append(characters)
40+
41+
if (len(numbers_row_array) > 0):
42+
print(numbers_row_array)
43+
else:
44+
print(lines, words, characters, file_path)
45+
total_lines += lines
46+
total_words += words
47+
total_characters += characters
48+
49+
if (len(files_array) > 1):
50+
print(total_lines, total_words, total_characters, 'total')

0 commit comments

Comments
 (0)