Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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
  • Loading branch information
DontEatThemCookies authored Mar 12, 2022
commit 5566d58aed63e1aa226669cb324049c565fba658
37 changes: 14 additions & 23 deletions Cat/cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
The 'cat' Program Implemented in Python 3

The Unix 'cat' utility reads the contents
of file(s) and 'conCATenates' into stdout.
If it is run without any filename(s) given,
then the program reads from standard input,
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
Expand All @@ -14,55 +14,46 @@
utility. Compatible with Python 3.6 or higher.

Syntax:
python3 cat.py [filename1 filename2 etcetera]
Separate filenames with spaces as usual.
python3 cat.py [filename1] [filename2] etc...
Separate filenames with spaces.

David Costell (DontEatThemCookies on GitHub)
v1 - 02/06/2022
v2 - 03/12/2022
"""
import sys


def with_files(files):
"""Executes when file(s) is/are specified."""
file_contents = []
try:
# Read the files' contents and store their contents
for file in files:
with open(file) as f:
file_contents.append(f.read())
# 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)
print(f"cat: error reading files ({err})")
exit(print(f"cat: error reading files ({err})"))

# Write the contents of all files into the standard output stream
# 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:
# Loop getting input then outputting the input.
# Get input, output the input, repeat
while True:
inp = input()
print(inp)
# Gracefully handle Ctrl + C and Ctrl + D
print(input())
# Graceful exit for Ctrl + C, Ctrl + D
except KeyboardInterrupt:
exit()
except EOFError:
exit()


def main():
"""Entry point of the cat program."""
try:
# Read the arguments passed to the program
file = sys.argv[1:]
with_files(file)
with_files(sys.argv[1:])
except IndexError:
no_files()


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions Cat/text_b.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ The knights who say ni!
Lines
of
Text!
This file does not end with a newline.
3 changes: 3 additions & 0 deletions Cat/text_c.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
I am the beginning of yet another text file.

Spam and eggs.