11import argparse
2+
23parser = argparse .ArgumentParser (
34 prog = "my-wc" ,
45 description = "Simple wc clone with -l and -w options" ,
56)
7+
8+
9+
10+
11+ def read_file (file_path ):
12+ with open (file_path , "r" ) as file :
13+ return file .read ()
14+
15+
16+ def count_text (text ):
17+ line_count = text .count ("\n " )
18+ word_count = len (text .split ())
19+ char_count = len (text )
20+ return line_count , word_count , char_count
21+
22+
23+
24+
625lineCounter = 0
726wordCounter = 0
827charCounter = 0
28+
929parser .add_argument ("-l" , action = "store_true" , help = "count lines" )
1030parser .add_argument ("-w" , action = "store_true" , help = "count words" )
1131parser .add_argument ("-c" , action = "store_true" , help = "count characters" )
1232parser .add_argument ("path" , nargs = "+" , default = "." , help = "The file to count" )
1333args = parser .parse_args ()
1434
15- if not args .l and not args .w and not args .c :
16- for file_path in args .path :
17- with open (file_path , "r" ) as f :
18- content = f .read ()
19- arrText = content .split ("\n " )
20- lineCounter += len (arrText )
21- arrWords = content .split ()
22- wordCounter += len (arrWords )
23- charCounter += len (content )
24- print ("Line count:" , lineCounter ,"lines" )
25- print ("Word count:" , wordCounter ,"words" )
26- print ("Character count:" , charCounter ,"characters" )
27- elif args .l and args .w :
28- for file_path in args .path :
29- with open (file_path , "r" ) as f :
30- content = f .read ()
31- arrText = content .split ("\n " )
32- lineCounter += len (arrText )
33- arrWords = content .split ()
34- wordCounter += len (arrWords )
35- print ("Line count:" , lineCounter ,"lines" )
36- print ("Word count:" , wordCounter ,"words" )
37- elif args .l and args .c :
38- for file_path in args .path :
39- with open (file_path , "r" ) as f :
40- content = f .read ()
41- arrText = content .split ("\n " )
42- lineCounter += len (arrText )
43- charCounter += len (content )
44- print ("Line count:" , lineCounter ,"lines" )
45- print ("Character count:" , charCounter ,"characters" )
46- elif args .l :
47- for file_path in args .path :
48- with open (file_path , "r" ) as f :
49- content = f .read ()
50- arrText = content .split ("\n " )
51- lineCounter += len (arrText )
52- print ("Line count:" , lineCounter ,"lines" )
53-
54- elif args .w :
55- for file_path in args .path :
56- with open (file_path , "r" ) as f :
57- content = f .read ()
58- arrText = content .split ()
59- wordCounter += len (arrText )
60- print ("Word count:" , wordCounter ,"words" )
61- elif args .c :
62- for file_path in args .path :
63- with open (file_path , "r" ) as f :
64- content = f .read ()
65- charCounter += len (content )
66- print ("Character count:" , charCounter ,"characters" )
35+
36+
37+ for path in args .path :
38+ text = read_file (path )
39+ lines , words , chars = count_text (text )
40+
41+ lineCounter += lines
42+ wordCounter += words
43+ charCounter += chars
44+
45+ if args .l :
46+ print (lines , path )
47+ elif args .w :
48+ print (words , path )
49+ elif args .c :
50+ print (chars , path )
51+ else :
52+ print (lines , words , chars , path )
53+
54+ if len (args .path ) > 1 and not args .l and not args .w and not args .c :
55+ print (lineCounter , wordCounter , charCounter , "total" )
0 commit comments