Skip to content

Commit 95b2ec8

Browse files
logic for file details complete
1 parent 7d85fc5 commit 95b2ec8

1 file changed

Lines changed: 37 additions & 2 deletions

File tree

  • implement-shell-tools/wc

implement-shell-tools/wc/wc.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,49 @@
11
import argparse
2+
import os
23

34
parser = argparse.ArgumentParser(
45
prog="py-wc",
56
description="A Python implementation of the Unix wc command",
67
)
78

8-
parser.add_argument("-l", "--lines", help="Print the newline counts")
9+
parser.add_argument("-l", "--lines", action="store_true", help="Print the newline counts")
910
parser.add_argument("-w", "--words", action="store_true", help="Print the word counts")
1011
parser.add_argument("-c", "--bytes", action="store_true", help="Print the byte counts")
11-
parser.add_argument("path", nargs="?", default=".", help="Directory to list")
12+
parser.add_argument("path", nargs="+", help="The file path to process")
1213

1314
args = parser.parse_args()
15+
file_paths = args.path
16+
17+
file_details_list = []
18+
19+
# Get file details
20+
for file_path in file_paths:
21+
with open(file_path, "r", encoding="utf-8") as f:
22+
content = f.read()
23+
24+
details = {
25+
"file_path": file_path,
26+
"file_size": os.path.getsize(file_path),
27+
"line_count": content.count("\n"), # matches real wc -l
28+
"word_count": len(content.split()), # split on whitespace
29+
}
30+
31+
file_details_list.append(details)
32+
33+
34+
print(file_details_list)
35+
36+
def get_line_count(text):
37+
return text.count("\n")
38+
39+
40+
def get_word_count(text):
41+
return len(text.split())
42+
43+
44+
45+
show_all = not (args.lines or args.words or args.bytes)
46+
47+
48+
# print(args)
1449

0 commit comments

Comments
 (0)