22import os
33
44parser = argparse .ArgumentParser (description = "wc command" )
5- parser .add_argument ("file" , help = "File or directory to read" )
5+ parser .add_argument ("files" , nargs = "+" , help = "File(s) or directory to read" )
66parser .add_argument ("-l" , dest = "lines" , action = "store_true" , help = "count the number of lines" )
77parser .add_argument ("-w" , dest = "words" , action = "store_true" , help = "count the number of words" )
88parser .add_argument ("-c" , dest = "chars" , action = "store_true" , help = "count the number of characters" )
@@ -29,14 +29,14 @@ def count_file(path):
2929 len (text .split ()),
3030 len (text ),
3131 )
32+ for file in args .files :
33+ if os .path .isdir (file ):
34+ for f in os .listdir (file ):
35+ path = os .path .join (file ,f )
36+ if os .path .isfile (path ):
37+ line_count , word_count , char_count = count_file (path )
38+ print (output (line_count , word_count , char_count , path , args ))
3239
33- if os .path .isdir (args .file ):
34- files = os .listdir (args .file )
35- for f in files :
36- path = os .path .join (args .file , f )
37- if os .path .isfile (path ):
38- line_count , word_count , char_count = count_file (path )
39- print (output (line_count , word_count , char_count , path , args ))
40- else :
41- line_count , word_count , char_count = count_file (args .file )
42- print (output (line_count , word_count , char_count , args .file , args ))
40+ else :
41+ line_count , word_count , char_count = count_file (file )
42+ print (output (line_count , word_count , char_count , file , args ))
0 commit comments