This does not crash:
import sys
print(len(sys.stdin.read()))
But this crashes:
import sys
print(len(sys.stdin.read()))
input('lol')
with output
2300
lolTraceback (most recent call last):
File "test018.py", line 3, in <module>
input('lol')
EOFError: EOF when reading a line
Questions:
- Why?
- How to do it right? My goal is to read some data from STDIN (e.g.
cat somefile | myscript), and then prompt the user on some matter (e.g.hey, does this look right?).
input()reads fromsys.stdinas well. Since you pipe, you can no longer useinput()to read input from the user. You can instead open/dev/ttydirectly for reading and writing.myscriptfrom the shell, it wouldn't touch stdin at all. After fork (before actually executing the program), the subprocess has all of the open file descriptors of the parent. If there is no piping, the shell just exec'smyscriptand it still has those fds. But since there is pipe incat somefile | myscript, after fork it closes stdin and substitutes the pipe's fd, then executes the program. The shell itself doesn't know when the pipe closes and doesn't really have an opportunity to substitute anything else. Its now just the business of the two connected programs.myscript "cat somefile". Your suggesting is interesting, but I don't know quite how to pull it off.