-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcat.py
More file actions
50 lines (40 loc) · 1.28 KB
/
cat.py
File metadata and controls
50 lines (40 loc) · 1.28 KB
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
48
49
50
import argparse
import sys
def parse_args():
parser = argparse.ArgumentParser(
description="Reads file(s) and writes them to the standard output",
)
parser.add_argument("paths", nargs="+", help="The file path(s) to process")
parser.add_argument(
"-n",
action="store_true",
dest="number_all",
help="Number the output lines, starting at 1.",
)
parser.add_argument(
"-b",
action="store_true",
dest="number_nonblank",
help="Number only non-blank output lines, starting at 1.",
)
return parser.parse_args()
def main():
args = parse_args()
try:
for path in args.paths:
line_num = 1
with open(path, "r", encoding="utf-8") as file:
for raw_line in file:
line = raw_line.rstrip("\n")
is_blank = line.strip() == ""
should_number = args.number_all or (
args.number_nonblank and not is_blank)
if should_number:
print(f"{line_num} {line}")
line_num += 1
else:
print(line)
except OSError as err:
print(err, file=sys.stderr)
return 0
main()