Skip to content

Commit e9f2a54

Browse files
committed
moving code into the helper function proccess_file
1 parent 82a6800 commit e9f2a54

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ def counter(item):
2424
def process_file(file_path):
2525
global total_lines, total_words, total_characters, file_count
2626

27+
with open(path, "r") as f:
28+
content = f.read()
29+
30+
stats = counter(content)
31+
32+
if args.line:
33+
print(f"{stats['lines']} {path}")
34+
elif args.word:
35+
print(f"{stats['words']} {path}")
36+
elif args.char:
37+
print(f"{stats['characters']} {path}")
38+
else:
39+
print(f"{stats['lines']} {stats['words']} {stats['characters']} {path}")
40+
2741

2842
total_lines = 0
2943
total_words = 0
@@ -33,17 +47,9 @@ def process_file(file_path):
3347

3448
for path in args.paths:
3549
if os.path.isfile(path):
36-
with open(path, "r") as f:
37-
content = f.read()
38-
stats = counter(content)
39-
if args.line:
40-
print(f"{stats['lines']} {path}")
41-
elif args.word:
42-
print(f"{stats['words']} {path}")
43-
elif args.char:
44-
print(f"{stats['characters']} {path}")
45-
else:
46-
print(f"{stats['lines']} {stats['words']} {stats['characters']} {path}")
50+
51+
52+
4753

4854
total_lines += stats['lines']
4955
total_words += stats['words']
@@ -57,7 +63,7 @@ def process_file(file_path):
5763
if os.path.isfile(file_path):
5864
with open(file_path, "r") as f:
5965
content = f.read()
60-
stats = counter(content)
66+
6167
if args.line:
6268
print(f"{stats['lines']} {file_path}")
6369
elif args.word:

0 commit comments

Comments
 (0)