Skip to content

Commit fbab9b0

Browse files
committed
Implement functionality for cat command in Python
1 parent 3f26722 commit fbab9b0

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

  • implement-shell-tools/cat

‎implement-shell-tools/cat/cat.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import argparse
2+
3+
parser = argparse.ArgumentParser(
4+
prog="check-for-cat",
5+
description="Implement my own version of cat in Python",
6+
)
7+
8+
parser.add_argument("paths", nargs="+", help="The file paths to process")
9+
parser.add_argument("-n", action="store_true", help="Number lines")
10+
parser.add_argument("-b", action="store_true", help="Number non-blank lines")
11+
12+
args = parser.parse_args()
13+
14+
paths = args.paths
15+
show_n = args.n
16+
show_b = args.b
17+
18+
line_number = 1
19+
20+
for path in paths:
21+
with open(path, "r", encoding="utf-8") as f:
22+
lines = f.readlines()
23+
24+
for line in lines:
25+
line = line.rstrip("\n")
26+
27+
if show_b:
28+
if line == "":
29+
print()
30+
else:
31+
numbered = str(line_number).rjust(6) + "\t" + line
32+
print(numbered)
33+
line_number += 1
34+
35+
elif show_n:
36+
numbered = str(line_number).rjust(6) + "\t" + line
37+
print(numbered)
38+
line_number += 1
39+
40+
else:
41+
print(line)

0 commit comments

Comments
 (0)