forked from CodeYourFuture/Module-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcat.py
More file actions
28 lines (21 loc) · 699 Bytes
/
cat.py
File metadata and controls
28 lines (21 loc) · 699 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
import sys
args = sys.argv[1:]
show_line_numbers = "-n" in args
number_non_empty = "-b" in args
files = [arg for arg in args if arg not in ["-n", "-b"]]
for file in files:
with open(file, "r") as f:
lines = f.readlines()
if number_non_empty:
count = 1
for line in lines:
if line.strip() != "":
print(f"{count:6} {line}", end="")
count += 1
else:
print(line, end="")
elif show_line_numbers:
for i, line in enumerate(lines, start=1):
print(f"{i:6} {line}", end="")
else:
print("".join(lines), end="")