Skip to content

Commit 6f8aa42

Browse files
author
joy
committed
kyclark#6 joy.y
1 parent 1a67f3d commit 6f8aa42

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

06_wc/wc.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Author : NowHappy <[email protected]>
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()

0 commit comments

Comments
 (0)