-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathcat.py
More file actions
47 lines (38 loc) · 930 Bytes
/
Copy pathcat.py
File metadata and controls
47 lines (38 loc) · 930 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
46
47
import argparse
parser = argparse.ArgumentParser(
prog="cat",
description="Concatenate and print files",
)
parser.add_argument(
"-n",
action="store_true",
help="Number all output lines"
)
parser.add_argument(
"-b",
action="store_true",
help="Number non-blank output lines"
)
parser.add_argument(
"paths",
nargs="+",
help="The file(s) to print",
)
args = parser.parse_args()
line_number = 1
for path in args.paths:
with open (path, "r") as f:
lines = f.readlines()
for line in lines:
is_blank = line.strip() == ""
if args.b:
if is_blank:
print(line, end="")
else:
print(f"{line_number:>6}\t{line}", end="")
line_number += 1
elif args.n:
print(f"{line_number:>6}\t{line}", end="")
line_number += 1
else:
print(line, end="")