close

[Solved] EOFError: EOF when reading a line

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error EOFError: EOF when reading a line in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How EOFError: EOF when reading a line Error Occurs?

Today I get the following error EOFError: EOF when reading a line in python.

How To Solve EOFError: EOF when reading a line Error ?

  1. How To Solve EOFError: EOF when reading a line Error ?

    To Solve EOFError: EOF when reading a line Error Notice the first print statement prints the entire string '1 2'. The second call to input() raises the EOFError (end-of-file error).

  2. EOFError: EOF when reading a line

    To Solve EOFError: EOF when reading a line Error Notice the first print statement prints the entire string '1 2'. The second call to input() raises the EOFError (end-of-file error).

Solution 1

convert your inputs to ints:

width = int(input())
height = int(input())

Solution 2

width, height = map(int, input().split())
def rectanglePerimeter(width, height):
   return ((width + height)*2)
print(rectanglePerimeter(width, height))

Running it like this produces:

% echo "1 2" | test.py
6

I suspect IDLE is simply passing a single string to your script. The first input() is slurping the entire string. Notice what happens if you put some print statements in after the calls to input():

width = input()
print(width)
height = input()
print(height)

Running echo "1 2" | test.py produces

1 2
Traceback (most recent call last):
  File "/home/unutbu/pybin/test.py", line 5, in <module>
    height = input()
EOFError: EOF when reading a line

Notice the first print statement prints the entire string '1 2'. The second call to input() raises the EOFError (end-of-file error).

So a simple pipe such as the one I used only allows you to pass one string. Thus you can only call input() once. You must then process this string, split it on whitespace, and convert the string fragments to ints yourself. That is what

width, height = map(int, input().split())

does.

Note, there are other ways to pass input to your program. If you had run test.py in a terminal, then you could have typed 1 and 2 separately with no problem. Or, you could have written a program with pexpect to simulate a terminal, passing 1 and 2 programmatically. Or, you could use argparse to pass arguments on the command line, allowing you to call your program with

test.py 1 2

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read