Skip to content

Commit 5566d58

Browse files
Cat program revised (v2)
Changes: * File argument syntax changed to imitate the real cat program: `python3 cat.py [filename1] [filename2] etc.` * Some comments modified * Other miscellaneous changes
1 parent f5047c2 commit 5566d58

File tree

3 files changed

+18
-23
lines changed

3 files changed

+18
-23
lines changed

Cat/cat.py

+14-23
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
The 'cat' Program Implemented in Python 3
33
44
The Unix 'cat' utility reads the contents
5-
of file(s) and 'conCATenates' into stdout.
6-
If it is run without any filename(s) given,
7-
then the program reads from standard input,
5+
of file(s) specified through stdin and 'conCATenates'
6+
into stdout. If it is run without any filename(s) given,
7+
then the program reads from standard input itself,
88
which means it simply copies stdin to stdout.
99
1010
It is fairly easy to implement such a program
@@ -14,55 +14,46 @@
1414
utility. Compatible with Python 3.6 or higher.
1515
1616
Syntax:
17-
python3 cat.py [filename1 filename2 etcetera]
18-
Separate filenames with spaces as usual.
17+
python3 cat.py [filename1] [filename2] etc...
18+
Separate filenames with spaces.
1919
2020
David Costell (DontEatThemCookies on GitHub)
21-
v1 - 02/06/2022
21+
v2 - 03/12/2022
2222
"""
2323
import sys
2424

25-
2625
def with_files(files):
2726
"""Executes when file(s) is/are specified."""
28-
file_contents = []
2927
try:
30-
# Read the files' contents and store their contents
31-
for file in files:
32-
with open(file) as f:
33-
file_contents.append(f.read())
28+
# Read each file's contents and store them
29+
file_contents = [contents for contents in [open(file).read() for file in files]]
3430
except OSError as err:
3531
# This executes when there's an error (e.g. FileNotFoundError)
36-
print(f"cat: error reading files ({err})")
32+
exit(print(f"cat: error reading files ({err})"))
3733

38-
# Write the contents of all files into the standard output stream
34+
# Write all file contents into the standard output stream
3935
for contents in file_contents:
4036
sys.stdout.write(contents)
4137

42-
4338
def no_files():
4439
"""Executes when no file(s) is/are specified."""
4540
try:
46-
# Loop getting input then outputting the input.
41+
# Get input, output the input, repeat
4742
while True:
48-
inp = input()
49-
print(inp)
50-
# Gracefully handle Ctrl + C and Ctrl + D
43+
print(input())
44+
# Graceful exit for Ctrl + C, Ctrl + D
5145
except KeyboardInterrupt:
5246
exit()
5347
except EOFError:
5448
exit()
5549

56-
5750
def main():
5851
"""Entry point of the cat program."""
5952
try:
6053
# Read the arguments passed to the program
61-
file = sys.argv[1:]
62-
with_files(file)
54+
with_files(sys.argv[1:])
6355
except IndexError:
6456
no_files()
6557

66-
6758
if __name__ == "__main__":
6859
main()

Cat/text_b.txt

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ The knights who say ni!
55
Lines
66
of
77
Text!
8+
This file does not end with a newline.

Cat/text_c.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
I am the beginning of yet another text file.
2+
3+
Spam and eggs.

0 commit comments

Comments
 (0)