I have a script that is designed to accept input piped in from stdin and then prompt the user for more input. Here is a contrived example illustrating what I mean:
import sys
# Get input from stdin
input_nums = [int(n.strip()) for n in sys.stdin]
# Prompt user
mult = int(raw_input("Enter a number by which to multiply your input: "))
for num in input_nums:
print num*mult
When I pipe data in from stdin, python interprets stdin as closed before it gets to raw_input
and it gives an EOFError: EOF when reading a line
:
[user]$ cat nums.txt
2
3
4
5
[user]$ cat nums.txt | python sample.py
Enter a number by which to multiply your input: Traceback (most recent call last):
File "sample.py", line 6, in <module>
mult = int(raw_input("Enter a number by which to multiply your input: "))
EOFError: EOF when reading a line
(Please don't worry about the useless use of cat
... its just a minimal example)
What I want to know is if there is a way to somehow separate reading sys.stdin and calling raw_input
so that I can both pipe in data and then prompt a user for input.
Updated to make it more clear what I really want by eliminating red herrings, and added traceback of EOFError
Result @TimPeter's solution worked for me, but I had to change "CON:" to "/dev/tty" since I'm on UNIX, not Windows.