|
2 | 2 | The 'cat' Program Implemented in Python 3 |
3 | 3 |
|
4 | 4 | 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, |
8 | 8 | which means it simply copies stdin to stdout. |
9 | 9 |
|
10 | 10 | It is fairly easy to implement such a program |
|
14 | 14 | utility. Compatible with Python 3.6 or higher. |
15 | 15 |
|
16 | 16 | 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. |
19 | 19 |
|
20 | 20 | David Costell (DontEatThemCookies on GitHub) |
21 | | -v1 - 02/06/2022 |
| 21 | +v2 - 03/12/2022 |
22 | 22 | """ |
23 | 23 | import sys |
24 | 24 |
|
25 | | - |
26 | 25 | def with_files(files): |
27 | 26 | """Executes when file(s) is/are specified.""" |
28 | | - file_contents = [] |
29 | 27 | 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]] |
34 | 30 | except OSError as err: |
35 | 31 | # 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})")) |
37 | 33 |
|
38 | | - # Write the contents of all files into the standard output stream |
| 34 | + # Write all file contents into the standard output stream |
39 | 35 | for contents in file_contents: |
40 | 36 | sys.stdout.write(contents) |
41 | 37 |
|
42 | | - |
43 | 38 | def no_files(): |
44 | 39 | """Executes when no file(s) is/are specified.""" |
45 | 40 | try: |
46 | | - # Loop getting input then outputting the input. |
| 41 | + # Get input, output the input, repeat |
47 | 42 | 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 |
51 | 45 | except KeyboardInterrupt: |
52 | 46 | exit() |
53 | 47 | except EOFError: |
54 | 48 | exit() |
55 | 49 |
|
56 | | - |
57 | 50 | def main(): |
58 | 51 | """Entry point of the cat program.""" |
59 | | - try: |
60 | | - # Read the arguments passed to the program |
61 | | - file = sys.argv[1:] |
62 | | - with_files(file) |
63 | | - except IndexError: |
| 52 | + # Read the arguments passed to the program |
| 53 | + if not sys.argv[1:]: |
64 | 54 | no_files() |
65 | | - |
| 55 | + else: |
| 56 | + with_files(sys.argv[1:]) |
66 | 57 |
|
67 | 58 | if __name__ == "__main__": |
68 | 59 | main() |
0 commit comments