Skip to content

Commit 140467f

Browse files
committed
get rid of repetetive code
1 parent 14fca64 commit 140467f

1 file changed

Lines changed: 9 additions & 23 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
args = parser.parse_args()
1616

17+
18+
total_lines = 0
19+
total_words = 0
20+
total_characters = 0
21+
file_count = 0
22+
23+
1724
def counter(item):
1825
lines = len(item.strip().split("\n"))
1926
words = len(item.split())
@@ -38,46 +45,25 @@ def process_file(file_path):
3845
else:
3946
print(f"{stats['lines']} {stats['words']} {stats['characters']} {file_path}")
4047

41-
4248
total_lines += stats['lines']
4349
total_words += stats['words']
4450
total_characters += stats['characters']
4551
file_count += 1
4652

4753

48-
total_lines = 0
49-
total_words = 0
50-
total_characters = 0
51-
file_count = 0
5254

5355

5456
for path in args.paths:
5557
if os.path.isfile(path):
56-
57-
58-
59-
58+
process_file(path)
6059
elif os.path.isdir(path):
6160
for file in os.listdir(path):
6261
file_path = os.path.join(path, file)
63-
6462
if os.path.isfile(file_path):
6563
with open(file_path, "r") as f:
6664
content = f.read()
6765

68-
if args.line:
69-
print(f"{stats['lines']} {file_path}")
70-
elif args.word:
71-
print(f"{stats['words']} {file_path}")
72-
elif args.char:
73-
print(f"{stats['characters']} {file_path}")
74-
else:
75-
print(f"{stats['lines']} {stats['words']} {stats['characters']} {file_path}")
76-
77-
total_lines += stats['lines']
78-
total_words += stats['words']
79-
total_characters += stats['characters']
80-
file_count += 1
66+
8167

8268

8369

0 commit comments

Comments
 (0)