Skip to content

Commit 0a4bec2

Browse files
author
Fatma Degirmenci
committed
add counter to display total line for multiple files
1 parent bd4aa9d commit 0a4bec2

File tree

1 file changed

+23
-1
lines changed
  • implement-shell-tools/wc

1 file changed

+23
-1
lines changed

implement-shell-tools/wc/wc.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
parser.add_argument("-c", dest="chars", action="store_true", help="count the number of characters")
99
args = parser.parse_args()
1010

11+
total_lines = 0
12+
total_words = 0
13+
total_chars = 0
14+
file_counter=0
15+
16+
17+
def totals(line_count,word_count,char_count):
18+
global total_lines,total_words,total_chars
19+
total_lines += line_count
20+
total_words += word_count
21+
total_chars += char_count
22+
1123
def output(line_count ,word_count ,char_count,filename,args):
1224
if not(args.lines or args.words or args.chars):
1325
return f"{line_count}\t{word_count}\t{char_count}\t{filename}"
@@ -34,9 +46,19 @@ def count_file(path):
3446
for f in os.listdir(file):
3547
path=os.path.join(file,f)
3648
if os.path.isfile(path):
49+
file_counter +=1
3750
line_count, word_count, char_count = count_file(path)
3851
print(output(line_count, word_count, char_count, path, args))
52+
totals(line_count, word_count, char_count)
53+
54+
3955

4056
else:
57+
file_counter +=1
4158
line_count, word_count, char_count = count_file(file)
42-
print(output(line_count, word_count, char_count, file, args))
59+
print(output(line_count, word_count, char_count, file, args))
60+
totals(line_count, word_count, char_count)
61+
62+
if file_counter > 1:
63+
print(output(total_lines, total_words, total_chars, "total", args))
64+

0 commit comments

Comments
 (0)