Skip to content

Commit 15d73be

Browse files
committed
Print helper
1 parent f0dc6b0 commit 15d73be

1 file changed

Lines changed: 29 additions & 4 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
words = 0
1313
bytes = 0
1414

15+
l = True
16+
w = True
17+
c = True
18+
1519
dict = {}
1620
adict = {}
1721

@@ -24,10 +28,31 @@
2428
f = open(file)
2529
adict[file] = f.read()
2630

31+
def print_helper(line, word, byte, file_name):
32+
text = [" "]
33+
if l == True:
34+
text.append(str(line))
35+
text.append(" ")
36+
if w == True:
37+
text.append(str(word))
38+
text.append(" ")
39+
if c == True:
40+
text.append(str(byte))
41+
text.append(" ")
42+
text.append(file_name)
43+
print("".join(text))
44+
2745
for f in dict:
2846
word_per_line = 0
2947
byte_per_line = 0
30-
for l in adict[f]:
31-
word_per_line += len(l.split())
32-
byte_per_line += len(l.encode("utf-8"))
33-
print(" " + str(len(dict[f]) - 1) + " " + str(word_per_line) + " " + str(byte_per_line) + " " + f)
48+
for line in dict[f]:
49+
word_per_line += len(line.split())
50+
for line in adict[f]:
51+
byte_per_line += len(line.encode("utf-8"))
52+
lines += len(dict[f]) - 1
53+
words += word_per_line
54+
bytes += byte_per_line
55+
print_helper(len(dict[f]) - 1, word_per_line, byte_per_line, f)
56+
57+
if len(args.path) > 1:
58+
print_helper(lines, words, bytes, "total")

0 commit comments

Comments
 (0)