-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathcat.py
More file actions
48 lines (37 loc) · 895 Bytes
/
cat.py
File metadata and controls
48 lines (37 loc) · 895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import argparse
parser = argparse.ArgumentParser(
prog="cat shell tool",
description="making cat tool with oython"
)
parser.add_argument(
"files",
nargs="+",
help="file to read"
)
parser.add_argument(
"-n",
"--number",
action="store_true",
help="add number to the lines"
)
parser.add_argument(
"-b",
"--number_nonblank",
action="store_true",
help="does not number the blank lines"
)
args = parser.parse_args()
number_line = 0
for file in args.files:
with open(file, "r") as f:
lines = f.readlines()
for line in lines:
if args.number_nonblank:
if line.strip():
number_line += 1
print(f"{number_line} {line}", end="")
elif args.number:
number_line += 1
print(f"{number_line} {line}", end="")
else:
print(line)