Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python raw_input following sys.stdin.read() throws EOFError

Tags:

python

a similar question has been asked before, but the answers suggested a workaround which is not applicable to my situation.

An email message is piped from mutt to a script, and is read from STDIN:

message = sys.stdin.read()
# message is parsed and URLs are printed as a list to choose from...
selected_index = raw_input('Which URL to open?')

I understand that raw_input() will get the EOF left by read(), but is there a way to 'reset' STDIN?

like image 275
janeden Avatar asked Nov 07 '11 09:11

janeden


People also ask

What does the Python Raw_input () function do?

Python raw_input function is used to get the values from the user. We call this function to tell the program to stop and wait for the user to input the values.

Why is Raw_input not working in Python?

The NameError: name 'raw_input' is not defined error is raised when you try to use the raw_input() method in Python 3. To fix this error, replace all instances of raw_input() with the input() function in your program.

What is Raw_input () in Python give an example?

a = input() will take the user input and put it in the correct type. Eg: if user types 5 then the value in a is integer 5. a = raw_input() will take the user input and put it as a string. Eg: if user types 5 then the value in a is string '5' and not an integer.


1 Answers

Have you tried this:

message = sys.stdin.read()
sys.stdin = open('/dev/tty')
selected_index = raw_input('Which URL to open?')

This works on Linux; maybe it will work for OSX too.

like image 59
unutbu Avatar answered Sep 20 '22 17:09

unutbu