Roshni Prusty
Roshni Prusty

Reputation: 21

How to resolve EOFError: EOF when reading a line?

Code:-

input_var=input("please enter the value")
print(input_var)

Error:- Enter a value

Runtime Exception Traceback (most recent call last): File "file.py", line 3, in n=input("Enter a value") EOFError: EOF when reading a line

I have started learning Python and tried to run this simple input and print statement. But its giving me the above error. I have tried running it on a online python compiler and it runs fine but when running on a compiler provided in a learning portal I am getting the above error.

Upvotes: 2

Views: 20508

Answers (1)

Masklinn
Masklinn

Reputation: 42197

I have tried running it on a online python compiler and it runs fine but when running on a compiler provided in a learning portal I am getting the above error.

input simply reads one line from the "standard input" stream. If the learning portal removes access to it (either closes it or sets it as a non-readable stream) then input is going to immediately get an error when it tries to read from the stream.

It simply means you can't use stdin for anything on that platform, so no input(), no sys.stdin.read(), … (so the resolution is "don't do that", it's pretty specifically forbidden)

In this specific case, the learning platform provides a non-readable stream as stdin e.g. /dev/null:

# test.py
input("test")
> python3 test.py </dev/null
Traceback (most recent call last):
  File "test.py", line 4, in <module>
    input("test")
EOFError: EOF when reading a line

if stdin were closed, you'd get a slightly different error:

> python3 test.py <&-
Traceback (most recent call last):
  File "test.py", line 4, in <module>
    input("test")
RuntimeError: input(): lost sys.stdin

Upvotes: 1

Related Questions