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
52 lines (39 loc) · 1.3 KB
/
cat.py
File metadata and controls
52 lines (39 loc) · 1.3 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
import argparse
import sys
def read_files(paths):
lines = []
for path in paths:
try:
with open(path, "r", encoding="utf-8") as f:
lines.extend(f.read().splitlines())
except OSError as e:
print(f"Error reading {path}: {e}", file=sys.stderr)
sys.exit(1)
return lines
def main():
parser = argparse.ArgumentParser(
prog="display-content-of-a-file",
description="cat is used to display the content of a file or print the content of a file.",
)
parser.add_argument("-n", action="store_true", help="number output lines")
parser.add_argument("-b", action="store_true", help="number non-empty output lines")
parser.add_argument("paths", nargs="+", help="The file path(s) to process")
options = parser.parse_args()
lines = read_files(options.paths)
if options.b:
line_num = 1
for line in lines:
if line.strip():
print(f"{line_num:6} {line}")
line_num += 1
else:
print(line)
elif options.n:
for i, line in enumerate(lines, start=1):
print(f"{i:6} {line}")
else:
for line in lines:
print(line)
if __name__ == "__main__":
main()
#python3 cat.py -b sample-files/*