Skip to content

Commit 84dcfb7

Browse files
Refactor wc function for clarity and error handling
1 parent 5476f45 commit 84dcfb7

1 file changed

Lines changed: 44 additions & 15 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.py

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,51 @@
11
import argparse
2+
import sys
23

34
def wc(path, count_lines, count_words, count_bytes):
45
"""Count lines, words, and bytes for a single file."""
56
try:
67
with open(path, 'r') as f:
78
content = f.read()
9+
810
lines = content.splitlines()
911
words = content.split()
10-
bytes_ = len(content.encode('utf-8'))
1112

12-
# Determine what to show
13+
line_count = len(lines)
14+
word_count = len(words)
15+
byte_count = len(content.encode('utf-8'))
16+
1317
if not any([count_lines, count_words, count_bytes]):
14-
count_lines = count_words = count_bytes = True
18+
count_lines = True
19+
count_words = True
20+
count_bytes = True
1521

1622
parts = []
17-
if count_lines: parts.append(str(len(lines)))
18-
if count_words: parts.append(str(len(words)))
19-
if count_bytes: parts.append(str(bytes_))
23+
24+
if count_lines:
25+
parts.append(str(line_count))
26+
27+
if count_words:
28+
parts.append(str(word_count))
29+
30+
if count_bytes:
31+
parts.append(str(byte_count))
2032

2133
print(' '.join(parts), path)
2234

23-
return (len(lines) if count_lines else 0,
24-
len(words) if count_words else 0,
25-
bytes_ if count_bytes else 0)
35+
return line_count, word_count, byte_count
36+
2637
except FileNotFoundError:
27-
print(f"wc: {path}: No such file or directory")
38+
print(
39+
f"wc: {path}: No such file or directory",
40+
file=sys.stderr
41+
)
2842
return (0, 0, 0)
43+
2944
except IsADirectoryError:
30-
print(f"wc: {path}: Is a directory")
45+
print(
46+
f"wc: {path}: Is a directory",
47+
file=sys.stderr
48+
)
3149
return (0, 0, 0)
3250

3351
def main():
@@ -38,8 +56,12 @@ def main():
3856
parser.add_argument('paths', nargs='+', help='Files to count')
3957
args = parser.parse_args()
4058

41-
total_lines = total_words = total_bytes = 0
59+
total_lines = 0
60+
total_words = 0
61+
total_bytes = 0
62+
4263
multiple_files = len(args.paths) > 1
64+
show_all = not any([args.l, args.w, args.c])
4365

4466
for path in args.paths:
4567
l, w, b = wc(path, args.l, args.w, args.c)
@@ -49,9 +71,16 @@ def main():
4971

5072
if multiple_files:
5173
parts = []
52-
if args.l or not any([args.l, args.w, args.c]): parts.append(str(total_lines))
53-
if args.w or not any([args.l, args.w, args.c]): parts.append(str(total_words))
54-
if args.c or not any([args.l, args.w, args.c]): parts.append(str(total_bytes))
74+
75+
if args.l or show_all:
76+
parts.append(str(total_lines))
77+
78+
if args.w or show_all:
79+
parts.append(str(total_words))
80+
81+
if args.c or show_all:
82+
parts.append(str(total_bytes))
83+
5584
print(' '.join(parts), 'total')
5685

5786
if __name__ == "__main__":

0 commit comments

Comments
 (0)