-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathcat.py
More file actions
38 lines (25 loc) · 929 Bytes
/
cat.py
File metadata and controls
38 lines (25 loc) · 929 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
import argparse
parser = argparse.ArgumentParser(
prog="display-file-content",
description="Output the content of a file to the terminal",
)
parser.add_argument("-n", help="Number the output lines", action="store_true")
parser.add_argument("-b", help="Number the non-blank output lines", action="store_true")
parser.add_argument("paths", help="The file(s)/path(s) to read from", nargs="+")
args = parser.parse_args()
line_number = 1
for path in args.paths:
with open(path, "r") as f:
for line in f:
line = line.rstrip("\n")
if args.n:
print(f"{line_number:>6} {line}")
line_number += 1
elif args.b:
if line != "":
print(f"{line_number} {line}")
line_number += 1
else:
print(f"{line}")
else:
print(f"{line}")