Skip to content

Commit 5d1476b

Browse files
Implement basic wc command in Python
1 parent 37c7132 commit 5d1476b

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import argparse
2+
3+
# Setup argument parser
4+
parser = argparse.ArgumentParser(
5+
prog="wc",
6+
description="Count lines, words, and characters"
7+
)
8+
parser.add_argument("paths", nargs='+', help="Files to count")
9+
10+
args = parser.parse_args()
11+
12+
for path in args.paths:
13+
# Read the file
14+
with open(path, "r") as f:
15+
content = f.read()
16+
17+
# Count lines, words, characters
18+
lines = len(content.rstrip('\n').split('\n'))
19+
words = len(content.split())
20+
chars = len(content)
21+
22+
print(f"{lines:8}{words:8}{chars:8} {path}")

0 commit comments

Comments
 (0)