Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: EOFError: EOF when reading a line

This may be repeated, but none of the existing answers solved my problem.

So, I'm using Python 2.7, and I get this error (title) whenever I try this:

number = int(raw_input('Number : '))

I tried this in Sublime Text 2, compileronline.com and in codecademy; it fails in the first 2 of this sites. It works on codecademy and in the terminal compiler, but I can't understand exactly why it is failing.

like image 728
Héctor Salazar Avatar asked Jul 20 '13 04:07

Héctor Salazar


People also ask

How do you fix EOF when reading a line in Python?

This error is sometimes experienced while using online IDEs. This occurs when we have asked the user for input but have not provided any input in the input box. We can overcome this issue by using try and except keywords in Python. This is called as Exception Handling.

What does EOF when reading a line mean in Python?

You should get an error like EOFError: EOF when reading a line. The acronym EOF stands for End Of File. This message literally means that the program called input() but failed to have any available input to read.

How does Python calculate EOF?

Python doesn't have built-in eof detection function but that functionality is available in two ways: f. read(1) will return b'' if there are no more bytes to read. This works for text as well as binary files. The second way is to use f.

What is EOF character in Python?

EOF stands for End Of File . This is the point in the program where the user cannot read the data anymore. It means that the program reads the whole file till the end.


2 Answers

The issue here is that Sublime text 2's console doesn't support input.

To fix this issue, you can install a package called SublimeREPL. SublimeREPL provides a Python interpreter that takes in input.

And as for compileronline.com, you need to provide input in the "STDIN Input" field on the lower right of the website.

like image 80
jh314 Avatar answered Oct 16 '22 11:10

jh314


try:
    value = raw_input()
    do_stuff(value) # next line was found 
except (EOFError):
   break #end of file reached

This seems to be proper usage of raw_input when dealing with the end of the stream of input from piped input. Refer this post

like image 30
DoOrDoNot Avatar answered Oct 16 '22 09:10

DoOrDoNot