Skip to content

Commit a79265c

Browse files
cat with python completed
1 parent 8a9931b commit a79265c

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

implement-shell-tools/cat/cat.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import argparse
2+
3+
parser = argparse.ArgumentParser(
4+
prog="cat shell tool",
5+
description="making cat tool with oython"
6+
)
7+
8+
parser.add_argument(
9+
"files",
10+
nargs="+",
11+
help="file to read"
12+
)
13+
14+
parser.add_argument(
15+
"-n",
16+
"--number",
17+
action="store_true",
18+
help="add number to the lines"
19+
)
20+
21+
parser.add_argument(
22+
"-b",
23+
"--number_nonblank",
24+
action="store_true",
25+
help="does not number the blank lines"
26+
)
27+
28+
args = parser.parse_args()
29+
30+
number_line = 0
31+
for file in args.files:
32+
with open(file, "r") as f:
33+
lines = f.readlines()
34+
35+
for line in lines:
36+
if args.number_nonblank:
37+
if line.strip():
38+
number_line += 1
39+
print(f"{number_line} {line}", end="")
40+
elif args.number:
41+
number_line += 1
42+
print(f"{number_line} {line}", end="")
43+
else:
44+
print(line)
45+
46+
47+
48+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)