88parser .add_argument ("-c" , dest = "chars" , action = "store_true" , help = "count the number of characters" )
99args = 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+
1123def 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