I want to run a python 3 script giving it a file via stdin or manually typing data.
E.g. let's suppose I want to print the contents of an input with one line. The script, called app.py would look something like this:
from sys import stdin
print("Input says:"+stdin.readline())
Then, I could run it in both of the following ways:
1. Passing a file as stdin
python app.py < input.txt
2. Prompt the user for the input
python app.py
My problem is that in both cases, after reading stdin I would like to prompt the user for some extra data. Following the before mentioned example it would be:
from sys import stdin
print("Input says:"+stdin.readline())
print("Did you like that? (Yes/No)")
ans = input() # This line is the issue!
if( ans == "Yes"):
print("You liked it!")
The commented line above works perfectly for case 2, but for case 1 it throws an EOFError: EOF when reading a line error because it tries to read from the file.
I would like to know if before that line I could do something like
sys.stdin = SOMETHING
Where SOMETHING represents the Windows Console input. I think that if I could do that, then both cases would work properly.
win32consoleapi.CONorCONIN$. Might work, depending on what exactly Python does with it.sys.stdin.