Skip to content

Commit cb49588

Browse files
committed
improving basic wc
1 parent a35d975 commit cb49588

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
import argparse
2+
import os
23

34
parser=argparse.ArgumentParser(prog="wc",usage="implement a simple wc in python")
45
parser.add_argument("path",nargs="+")
56
args=parser.parse_args()
67

7-
print(args.path)
8+
# print(args.path)
89
paths=args.path
9-
for path in paths :
10-
with open(path,"r") as f :
10+
words_count=0
11+
total_lines=0
12+
total_words=0
13+
total_bytes=0
14+
15+
for file in paths :
16+
with open(file,"r") as f :
17+
bytes_count=os.path.getsize(file)
1118
lines=f.readlines()
12-
print(len(lines) , path)
19+
lines_count=len(lines)
20+
for line in lines :
21+
words_count+=len(line.split())
22+
total_lines +=lines_count
23+
total_bytes += bytes_count
24+
total_words +=words_count
25+
print(lines_count ,words_count , bytes_count, file)
26+
words_count=0
27+
28+
print(total_lines,total_words,total_bytes,"total")

0 commit comments

Comments
 (0)