-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathcat.py
More file actions
51 lines (40 loc) · 1.25 KB
/
cat.py
File metadata and controls
51 lines (40 loc) · 1.25 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
51
import argparse
parser = argparse.ArgumentParser(
prog="py-cat",
description="A Python implementation of the Unix cat command",
)
parser.add_argument("-n", "--number", action="store_true", help="Number all output lines")
parser.add_argument("-b", "--numberNonBlank", action="store_true", help="Numbers only non-empty lines. Overrides -n option")
parser.add_argument("path", nargs="+", help="The file path to process")
args = parser.parse_args()
def number_all(lines):
numbered = []
for i, line in enumerate(lines):
numbered.append(f"{i + 1:>6}\t{line}")
return numbered
def number_non_blank(lines):
numbered = []
counter = 1
for line in lines:
if line == "":
numbered.append(line)
else:
numbered.append(f"{counter:>6}\t{line}")
counter += 1
return numbered
# Read and concatenate file contents
content = ""
for path in args.path:
with open(path, "r") as f:
content += f.read()
if content.endswith("\n"):
content = content[:-1]
# Split content into lines
lines = content.split("\n")
# Output logic
if args.numberNonBlank:
print("\n".join(number_non_blank(lines)))
elif args.number:
print("\n".join(number_all(lines)))
else:
print("\n".join(lines))