1+ import os
2+ import argparse
3+ import sys
4+
5+ parser = argparse .ArgumentParser (
6+ prog = "wc" ,
7+ description = "Counts words in a file that contain a particular character" ,
8+ )
9+
10+ parser .add_argument ("-l" ,action = 'store_true' , help = "The number of lines in each input file is written to the standard output." )
11+ parser .add_argument ("-w" , action = 'store_true' , help = "The number of words in each input file is written to the standard output." )
12+ parser .add_argument ("-c" , action = 'store_true' , help = "The number of bytes in each input file is written to the standard output." )
13+ parser .add_argument ("path" , nargs = "+" , help = "The path to search" )
14+
15+ args = parser .parse_args ()
16+
17+ if (not args .w and not args .c and not args .l ) :
18+ args .w = args .c = args .l = True
19+ total = []
20+ for path in args .path :
21+ output = []
22+ if args .l or args .w :
23+ with open (path ) as file :
24+ lines = file .readlines ()
25+ # lines count
26+ if args .l :
27+ num_lines = len (lines )
28+ output .append (num_lines )
29+ # word count
30+ if args .w :
31+ word_count = 0
32+ for line in lines :
33+ lin = line .rstrip ()
34+ wds = lin .split ()
35+ word_count += len (wds )
36+
37+ output .append (word_count )
38+
39+
40+ if args .c :
41+ file_size = os .path .getsize (path )
42+ output .append (file_size )
43+
44+ if len (args .path ) > 1 :
45+ total .append (output .copy ())
46+ output .append (path )
47+ string_list = map (str , output )
48+ print (" " .join (string_list ))
49+ if len (args .path ) > 1 :
50+ result = [sum (i ) for i in zip (* total )]
51+ string_result_list = map (str , result )
52+ print (" " .join (string_result_list ), " total" )
53+
54+
55+
0 commit comments