File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python3
2+ """
3+ Author : NowHappy <rlfmalehd@gmail.com>
4+ Date : 2021-10-04
5+ Purpose: Rock the Casbah
6+ """
7+
8+ import argparse
9+ import sys
10+
11+ # --------------------------------------------------
12+ def get_args ():
13+ """Get command-line arguments"""
14+
15+ parser = argparse .ArgumentParser (
16+ description = 'Emulate wc (word count)' ,
17+ formatter_class = argparse .ArgumentDefaultsHelpFormatter )
18+
19+ parser .add_argument ('file' ,
20+ help = 'Input file(s)' ,
21+ metavar = 'FILE' ,
22+ nargs = '*' ,
23+ type = argparse .FileType ('rt' ),
24+ default = [sys .stdin ])
25+
26+ return parser .parse_args ()
27+
28+
29+ # --------------------------------------------------
30+ def main ():
31+ """Make a jazz noise here"""
32+
33+ args = get_args ()
34+ t_num_lines = t_num_words = t_num_bytes = 0
35+
36+ for fh in args .file :
37+ num_lines = num_words = num_bytes = 0
38+ for line in fh :
39+ num_lines += 1
40+ num_words += len (line .split ())
41+ num_bytes += len (line )
42+ print (f'{ num_lines :8} { num_words :8} { num_bytes :8} { fh .name } ' )
43+ t_num_lines += num_lines
44+ t_num_words += num_words
45+ t_num_bytes += num_bytes
46+
47+ if len (args .file ) > 1 :
48+ print (f'{ t_num_lines :8} { t_num_words :8} { t_num_bytes :8} total' )
49+
50+
51+ # --------------------------------------------------
52+ if __name__ == '__main__' :
53+ main ()
You can’t perform that action at this time.
0 commit comments