File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import argparse
2+ import os
3+
4+ parser = argparse .ArgumentParser (description = "Python implementation of wc command" )
5+
6+ parser .add_argument ("files" , nargs = "+" , help = "Files to process" )
7+
8+ args = parser .parse_args ()
9+
10+ total_lines = 0
11+ total_words = 0
12+ total_bytes = 0
13+
14+ multiple_files = len (args .files ) > 1 # to store totals if muliple files
15+
16+ for file in args .files :
17+ if not os .path .isfile (file ):
18+ print (f"wc: { file } : No such file or directory" )
19+ continue
20+
21+ with open (file , "r" , encoding = "utf-8" ) as f :
22+ content = f .read ()
23+ lines = content .count ("\n " )
24+ words = len (content .split ())
25+ tbytes = len (content .encode ("utf-8" ))
26+
27+
28+ total_lines += lines
29+ total_words += words
30+ total_bytes += tbytes
31+
32+ print (f"{ lines :>7} { words :>7} { tbytes :>7} { file } " ) # to print data from per life
33+
34+ #to print total output
35+ if multiple_files :
36+ print (f"{ total_lines :>7} { total_words :>7} { total_bytes :>7} total" )
37+
38+
39+
You can’t perform that action at this time.
0 commit comments