generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathcat.py
More file actions
executable file
·61 lines (45 loc) · 1.38 KB
/
cat.py
File metadata and controls
executable file
·61 lines (45 loc) · 1.38 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
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
import sys
import os
global_line_counter = 1
def print_file(file_path, options):
global global_line_counter
try:
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
lines = content.split("\n")
if lines and lines[-1] == "":
lines.pop()
for line in lines:
prefix = ""
should_number = (
options["number_mode"] == "all" or
(options["number_mode"] == "non-empty" and line.strip() != "")
)
if should_number:
prefix = f"{global_line_counter:6}\t"
global_line_counter += 1
sys.stdout.write(prefix + line + "\n")
except Exception as e:
print(f"cat: {file_path}: {e}", file=sys.stderr)
sys.exit(1)
def main():
global global_line_counter
args = sys.argv[1:]
options = {"number_mode": "off"}
files = []
for arg in args:
if arg == "-n":
options["number_mode"] = "all"
elif arg == "-b":
options["number_mode"] = "non-empty"
else:
files.append(arg)
if not files:
print("cat: missing file operand", file=sys.stderr)
sys.exit(1)
for file_path in files:
global_line_counter = 1
print_file(file_path, options)
if __name__ == "__main__":
main()