Skip to content

Commit 0318f77

Browse files
committed
finished cat.py
1 parent b5089a2 commit 0318f77

File tree

1 file changed

+29
-0
lines changed
  • implement-shell-tools/cat

1 file changed

+29
-0
lines changed

implement-shell-tools/cat/cat.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import argparse
2+
3+
parser = argparse.ArgumentParser()
4+
parser.add_argument("files", nargs="+")
5+
parser.add_argument("-n", action="store_true")
6+
parser.add_argument("-b", action="store_true")
7+
8+
args = parser.parse_args()
9+
10+
line_number = 1
11+
12+
for file in args.files:
13+
try:
14+
f = open(file, "r")
15+
16+
for line in f:
17+
if args.b and line.strip() != "":
18+
print(f" {line_number}", line, end="")
19+
line_number += 1
20+
elif args.n and not args.b:
21+
print(f" {line_number}", line, end="")
22+
line_number += 1
23+
else:
24+
print(line, end="")
25+
26+
f.close()
27+
28+
except:
29+
print("cat:", file, "not found")

0 commit comments

Comments
 (0)