Litwisha
Litwisha

Reputation: 53

What's wrong with raw_input() (EOFError: EOF when reading a line)

I am using python 2.7.6, and when calling raw_input(), there is an exception:

flight_name = raw_input('\nEnter the name of Flight: ')

I found the solution, but why there appears such exception? Is it a bag?

try:
      flight_name = raw_input('\nEnter the name of Flight: ')
except (EOFError):
      break

I'm using PyCharm.

Upvotes: 4

Views: 14816

Answers (3)

mcstar
mcstar

Reputation: 581

This answer

sys.stdin = open('/dev/tty')
answer = raw_input('Commit anyway? [N/y] ')

is useful if you've previously read from sys.stdin in your code. In that case, the raw_input line will throw the EOF error without waiting for user input, if you set the sys.stdin = open('/dev/tty') just before raw_input, it resets the stdin and allows the user input to work. (tested in python 2.7) with

## test.py
import sys
for line in sys.stdin:
  pass
#sys.stdin = open('/dev/tty')
raw_input("Are you sure?")

echo 'abc' | python test.py

Which raises:

EOFError: EOF when reading a line

Until you uncomment the 2nd to last line.

Upvotes: 2

Pylinux
Pylinux

Reputation: 11816

You could use:

sys.stdin = open('/dev/tty')
answer = raw_input('Commit anyway? [N/y] ')

source: https://stackoverflow.com/a/7437724/1465640

Upvotes: 3

tayfun
tayfun

Reputation: 3135

You're trying to read something from standard input but you're not getting anything, only the EOF. This actually lets you know the user is not providing any input and you can do other things for this condition.

Upvotes: 0

Related Questions