-
Notifications
You must be signed in to change notification settings - Fork 12.5k
/
Copy pathcat.py
59 lines (50 loc) · 1.74 KB
/
cat.py
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
"""
The 'cat' Program Implemented in Python 3
The Unix 'cat' utility reads the contents
of file(s) specified through stdin and 'conCATenates'
into stdout. If it is run without any filename(s) given,
then the program reads from standard input itself,
which means it simply copies stdin to stdout.
It is fairly easy to implement such a program
in Python, and as a result countless examples
exist online. This particular implementation
focuses on the basic functionality of the cat
utility. Compatible with Python 3.6 or higher.
Syntax:
python3 cat.py [filename1] [filename2] etc...
Separate filenames with spaces.
David Costell (DontEatThemCookies on GitHub)
v2 - 03/12/2022
"""
import sys
def with_files(files):
"""Executes when file(s) is/are specified."""
try:
# Read each file's contents and store them
file_contents = [contents for contents in [open(file).read() for file in files]]
except OSError as err:
# This executes when there's an error (e.g. FileNotFoundError)
exit(print(f"cat: error reading files ({err})"))
# Write all file contents into the standard output stream
for contents in file_contents:
sys.stdout.write(contents)
def no_files():
"""Executes when no file(s) is/are specified."""
try:
# Get input, output the input, repeat
while True:
print(input())
# Graceful exit for Ctrl + C, Ctrl + D
except KeyboardInterrupt:
exit()
except EOFError:
exit()
def main():
"""Entry point of the cat program."""
# Read the arguments passed to the program
if not sys.argv[1:]:
no_files()
else:
with_files(sys.argv[1:])
if __name__ == "__main__":
main()