Skip to content

Commit d462b5a

Browse files
committed
cat command complete
1 parent 1fa1221 commit d462b5a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import argparse
2+
3+
parser = argparse.ArgumentParser(
4+
prog="cat",
5+
description="Read, display, and concatenate text files.",
6+
)
7+
8+
parser.add_argument("-n", "--number", action="store_true", help="Number all output lines.")
9+
parser.add_argument("-b", "--number-nonblank", action="store_true", help="Number non-blank output lines.")
10+
parser.add_argument("paths", nargs="+", help="The file(s) to read.")
11+
12+
args = parser.parse_args()
13+
14+
for path in args.paths:
15+
try:
16+
with open(path, mode='r', encoding='utf-8') as f:
17+
lines = f.readlines()
18+
except Exception as err:
19+
print(f"Error reading file '{path}': {err}")
20+
continue
21+
22+
line_num = 1
23+
24+
for line in lines:
25+
if args.number_nonblank:
26+
if line.strip() != "":
27+
print(f"{line_num:6}\t{line}", end="")
28+
line_num += 1
29+
else:
30+
print(line, end="")
31+
elif args.number:
32+
print(f"{line_num:6}\t{line}", end="")
33+
line_num += 1
34+
else:
35+
print(line, end="")
36+
37+

0 commit comments

Comments
 (0)