Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3: EOF when reading a line (Sublime Text 2 is angry)

while True:     reply = input('Enter text')     if reply == 'stop': break     print(reply.upper()) 

The result was:

Enter text:Traceback (most recent call last):   File "C:\PythonProjects\5.py", line 2, in <module>     reply = input('Enter text:') EOFError: EOF when reading a line [Finished in 0.2s with exit code 1] 

It is only in Sublime Text 2. I tried IDLE, tried command line, everything is perfect. Why should Subleme shout at me?

By the way, maybe you could also explain my what EOF may mean in such situation. Of course, I have read in the documentation, that if EOF is read from input, the appropriate error is raised. I would like to model this situation. Is input only about the keyboard? If yes, what combination of keys should I input to get EOF?

Thank you in advance.

like image 849
Kifsif Avatar asked Sep 22 '12 21:09

Kifsif


People also ask

How do you fix EOF in Python 3?

The best practice to avoid EOF in python while coding on any platform is to catch the exception, and we don't need to perform any action so, we just pass the exception using the keyword “pass” in the “except” block.

How do you fix EOFError 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.

Why do I keep getting EOF error in Python?

Unexpected EOF implies that the interpreter has reached the end of our program before executing all the code. This error is likely to occur when: we fail to declare a statement for loop ( while / for ) we omit the closing parenthesis or curly bracket in a block of code.

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.


2 Answers

I had the same problem. The problem with the Sublime Text's default console is that it does not support input.

To solve it, you have to install a package called SublimeREPL. SublimeREPL provides a Python interpreter which accepts input.

There is an article that explains the solution in detail.

GitHub page for SublimeREPL

like image 192
Raghav Mujumdar Avatar answered Oct 09 '22 04:10

Raghav Mujumdar


help(input) shows what keyboard shortcuts produce EOF, namely, Unix: Ctrl-D, Windows: Ctrl-Z+Return:

input([prompt]) -> string

Read a string from standard input. The trailing newline is stripped. If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError. On Unix, GNU readline is used if enabled. The prompt string, if given, is printed without a trailing newline before reading.

You could reproduce it using an empty file:

$ touch empty $ python3 -c "input()" < empty Traceback (most recent call last):   File "<string>", line 1, in <module> EOFError: EOF when reading a line 

You could use /dev/null or nul (Windows) as an empty file for reading. os.devnull shows the name that is used by your OS:

$ python3 -c "import os; print(os.devnull)" /dev/null 

Note: input() happily accepts input from a file/pipe. You don't need stdin to be connected to the terminal:

$ echo abc | python3 -c "print(input()[::-1])" cba 

Either handle EOFError in your code:

try:     reply = input('Enter text') except EOFError:     break 

Or configure your editor to provide a non-empty input when it runs your script e.g., by using a customized command line if it allows it: python3 "%f" < input_file

like image 27
jfs Avatar answered Oct 09 '22 03:10

jfs