Skip to content

Commit b6de70f

Browse files
committed
changes were done
1 parent d4812db commit b6de70f

1 file changed

Lines changed: 33 additions & 61 deletions

File tree

  • implement-shell-tools/wc

‎implement-shell-tools/wc/wc.py‎

Lines changed: 33 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,60 @@
1-
import sys
21

2+
import sys
33

4-
# Get everything the user typed after wc.py
54
arguments = sys.argv[1:]
6-
7-
8-
# These are our options
95
count_lines = False
106
count_words = False
117
count_characters = False
12-
13-
14-
# Store file names here
158
filenames = []
169

17-
18-
# Check every argument
1910
for argument in arguments:
20-
2111
if argument == "-l":
2212
count_lines = True
23-
2413
elif argument == "-w":
2514
count_words = True
26-
2715
elif argument == "-c":
2816
count_characters = True
29-
3017
else:
3118
filenames.append(argument)
3219

33-
34-
35-
# If the user did not give any flag,
36-
# show all counts like normal wc
37-
if (not count_lines
38-
and not count_words
39-
and not count_characters):
40-
20+
if not count_lines and not count_words and not count_characters:
4121
count_lines = True
4222
count_words = True
4323
count_characters = True
4424

25+
total_lines = 0
26+
total_words = 0
27+
total_characters = 0
4528

46-
47-
# Go through every file
4829
for filename in filenames:
49-
50-
# Open the file
5130
with open(filename, "r") as file:
52-
53-
# Read the whole file
5431
content = file.read()
5532

56-
57-
# Count lines
58-
lines = len(content.splitlines())
59-
60-
61-
# Count words
62-
words = len(content.split())
63-
64-
65-
# Count characters
66-
characters = len(content)
67-
68-
69-
70-
# Prepare the answer
71-
answer = ""
72-
73-
74-
if count_lines:
75-
answer += str(lines) + " "
76-
77-
78-
if count_words:
79-
answer += str(words) + " "
80-
81-
82-
if count_characters:
83-
answer += str(characters) + " "
84-
85-
86-
87-
# Print the result
88-
print(answer + filename)
33+
lines = len(content.splitlines())
34+
words = len(content.split())
35+
characters = len(content)
36+
37+
total_lines += lines
38+
total_words += words
39+
total_characters += characters
40+
41+
answer = ""
42+
if count_lines:
43+
answer += str(lines) + " "
44+
if count_words:
45+
answer += str(words) + " "
46+
if count_characters:
47+
answer += str(characters) + " "
48+
49+
print(answer + filename)
50+
51+
if len(filenames) > 1:
52+
total = ""
53+
if count_lines:
54+
total += str(total_lines) + " "
55+
if count_words:
56+
total += str(total_words) + " "
57+
if count_characters:
58+
total += str(total_characters) + " "
59+
60+
print(total + "total")

0 commit comments

Comments
 (0)