File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ import argparse
2+
3+ parser = argparse .ArgumentParser (
4+ prog = "wc shell tool" ,
5+ description = "making wc tool with python"
6+ )
7+
8+ parser .add_argument (
9+ "files" ,
10+ nargs = "+" ,
11+ help = "file or files to work on"
12+ )
13+ parser .add_argument (
14+ "-w" ,
15+ action = "store_true" ,
16+ help = "counting words"
17+ )
18+ parser .add_argument (
19+ "-l" ,
20+ action = "store_true" ,
21+ help = "counting lines"
22+ )
23+ parser .add_argument (
24+ "-c" ,
25+ action = "store_true" ,
26+ help = "counting char"
27+ )
28+
29+ args = parser .parse_args ()
30+
31+ for file in args .files :
32+ try :
33+ with open (file , "r" ) as f :
34+ content = f .read ()
35+ word_count = len (content .split ())
36+ line_count = content .count ("\n " )
37+ char_count = len (content )
38+ if args .w :
39+ print (f"word count of file { file } is : " , word_count )
40+ elif args .l :
41+
42+ print (f"line count of file { file } is : " , line_count )
43+ elif args .c :
44+
45+ print (f"char count of file { file } is : " , char_count )
46+ else :
47+ print (f"line: { line_count } , word: { word_count } , char: { char_count } , { file } " )
48+ except FileNotFoundError :
49+ print (f"wc: { file } : file or directory not found" )
50+
51+
You can’t perform that action at this time.
0 commit comments