File tree Expand file tree Collapse file tree 1 file changed +13
-6
lines changed Expand file tree Collapse file tree 1 file changed +13
-6
lines changed Original file line number Diff line number Diff line change @@ -26,18 +26,19 @@ def get_parser(self):
26
26
def run (self ):
27
27
args = self .get_args ()
28
28
29
+ settings = ['lines' , 'bytes' , 'words' ]
30
+
29
31
# if none are chosen, then choose all (per GNU behavior)
30
- if not any ([args ['lines' ], args ['bytes' ], args ['words' ]]):
31
- args ['lines' ] = True
32
- args ['bytes' ] = True
33
- args ['words' ] = True
32
+ if not any (args [i ] for i in settings ):
33
+ for i in settings :
34
+ args [i ] = True
34
35
35
- totals = {'words' : 0 , 'lines' : 0 , 'chars' : 0 }
36
+ totals = {k : 0 for k in settings }
36
37
37
38
for i in args ['files' ]:
38
39
try :
39
40
with open (i , 'rb' ) as f :
40
- file_metrics = {'lines' : 0 , 'bytes' : 0 , 'words' : 0 }
41
+ file_metrics = {k : 0 for k in settings }
41
42
42
43
curr_char = f .read (1 )
43
44
while curr_char != b'' :
@@ -46,10 +47,16 @@ def run(self):
46
47
if curr_char in b'\n \t ' : file_metrics ['words' ] += 1
47
48
curr_char = f .read (1 )
48
49
50
+ for k in settings :
51
+ totals [k ] += file_metrics [k ]
52
+
49
53
print ("{} {} {} {}" .format (file_metrics ['lines' ], file_metrics ['words' ], file_metrics ['bytes' ], i ))
50
54
51
55
except FileNotFoundError :
52
56
self .file_not_found_error (i )
53
57
54
58
except IsADirectoryError :
55
59
self .directory_error (i )
60
+
61
+ if len (args ['files' ]) > 1 :
62
+ print ("{} {} {} total" .format (totals ['lines' ], totals ['words' ], totals ['bytes' ]))
You can’t perform that action at this time.
0 commit comments