-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathcat.py
More file actions
executable file
·45 lines (37 loc) · 1.51 KB
/
cat.py
File metadata and controls
executable file
·45 lines (37 loc) · 1.51 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
#!/usr/bin/env python3
import argparse
# implement-shell-tools/cat/cat.py
def main():
parser = argparse.ArgumentParser(
prog="cat",
description="Implements a simple version of the 'cat' command to read and display file contents."
)
parser.add_argument("-n", help="Number lines", action="store_true")
parser.add_argument("-b", help="Number non-blank lines", action="store_true")
parser.add_argument("path", nargs="+", help="The file to search")
args = parser.parse_args()
line_number = 1
non_blank_line_number = 1
for file_path in args.path:
try:
with open(file_path, 'r') as file:
content = file.read()
lines = content.split("\n")
for i, line in enumerate(lines):
if i == len(lines) - 1 and line == "": # Skip the last empty line if it exists
break
prefix = ""
if args.n:
prefix = str(line_number).rjust(6) + " "
elif args.b and line != "":
prefix = str(non_blank_line_number).rjust(6) + " "
print(prefix + line)
line_number += 1
if line != "":
non_blank_line_number += 1
except FileNotFoundError:
print(f"cat: {file_path}: No such file or directory")
except Exception as e:
print(f"cat: {file_path}: {e}")
if __name__ == "__main__":
main()