janeden
janeden

Reputation: 461

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

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?

Upvotes: 8

Views: 3185

Answers (2)

krishnang
krishnang

Reputation: 698

Try to reset STDIN using sys.stdin.seek(0)

Reference: http://docs.python.org/library/fileinput.html

Upvotes: 0

unutbu
unutbu

Reputation: 879591

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.

Upvotes: 6

Related Questions