-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcat.py
More file actions
executable file
·43 lines (35 loc) · 779 Bytes
/
cat.py
File metadata and controls
executable file
·43 lines (35 loc) · 779 Bytes
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
#!/usr/bin/env python3
# -*- coding: utf8 -*-
"""
Simple cat implementatin in Python.
"""
import sys
def cat(fobj):
"""
Read and print content of a file object.
"""
for line in fobj:
print(line, end="")
def main(argv=None):
"""
Main function.
"""
argv = argv or sys.argv
retcode = 0
if len(argv) == 1:
try:
cat(sys.stdin)
except KeyboardInterrupt:
print()
retcode = 130
return retcode
for filename in argv[1:]:
try:
with open(filename, "r") as fobj:
cat(fobj)
except IOError as ex:
sys.stderr.write("%s\n" % ex)
retcode = 1
return retcode
if __name__ == '__main__':
sys.exit(main())