Skip to content

Commit 541562d

Browse files
committed
looping over the files and concatenate it
1 parent e6867b7 commit 541562d

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
@@ -11,5 +11,27 @@ def main():
1111
parser.add_argument("files", nargs="+", help="files to read")
1212
options = parser.parse_args()
1313

14+
total_lines = total_words = total_bytes = 0
15+
16+
for file_path in options.files:
17+
try:
18+
with open(file_path, "r", encoding="utf-8") as f:
19+
data = f.read()
20+
21+
lines = data.count("\n")
22+
words = len(data.split())
23+
bytes_ = len(data.encode("utf-8"))
24+
25+
total_lines += lines
26+
total_words += words
27+
total_bytes += bytes_
28+
29+
print(format_output(lines, words, bytes_, os.path.basename(file_path), options))
30+
except Exception as e:
31+
print(f"Error reading {file_path}: {e}", file=sys.stderr)
32+
33+
if len(options.files) > 1:
34+
print(format_output(total_lines, total_words, total_bytes, "total", options))
35+
1436
if __name__ == "__main__":
1537
main()

0 commit comments

Comments
 (0)