/
cat2.py
46 lines (36 loc) · 1.23 KB
/
cat2.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
import os
import sys
from pathlib import Path
from typing import TextIO
def do_cat2(f: TextIO) -> None:
while True:
b = os.read(f.fileno(), 1)
n = os.write(sys.stdout.fileno(), b)
if n == 0:
break
def main():
argv = sys.argv
if len(argv) == 1:
do_cat2(sys.stdin)
else:
for arg in argv[1:]:
if os.path.exists(arg):
with open(arg) as f:
do_cat2(f)
else:
print(f'file "{arg}" does not exist', file=sys.stderr)
exit(1)
# もっと低レイヤーすぎると、整数を渡すことになるので扱いづらい
# ファイルがない場合 -1 じゃなくて、FileNotFoundErrorを出すのでイマイチ
# fd = os.open(arg, os.O_RDONLY)
# print(fd)
# pathlibの場合は、.fileno()とか使えないので注意ね
# file_path = Path(arg)
#
# if file_path.exists():
# with file_path.open(mode='r'):
# do_cat2(file_path)
# else:
# print(f'file "{file_path}" does not exist', file=sys.stderr)
if __name__ == '__main__':
main()