Dalalama231123
Dalalama231123

Reputation: 121

EOF Error occurs when trying to give a user input

I am learning python and have the following code in test.py:

string = input()
print(string)

Contrary to tutorials, it gives me an error:

    string = input()
EOFError: EOF when reading a line

I have no idea what I do wrong here! Any help appreciated!

Upvotes: 4

Views: 46394

Answers (3)

poiboi
poiboi

Reputation: 150

You seem to have an EOFError exception when running your code. EOFError is short for "End-of-File Error." This error occurs when Python has reached the end of user input without receiving any input.

The reason that EOFError occurs is that Python attempts to print out your input in variable string when no data is given.

Generally, this error shouldn't pop up when you press your Enter key as input as it registers the variable as a string in your code. It can be manually triggered with the following keystrokes:

  • Windows: Ctrl + Z -> Enter
  • macOS/Linux: Ctrl + D -> Enter

I'd recommend rerunning your program with the same code as it is not likely for an EOFError to appear. Try to avoid using the above keystrokes when the program requires input so the error doesn't appear.

If you don't want to see the error in your program, try handling the error and performing another action like printing a string. The code block I've typed prints "no data provided to input function" if an EOFError exception occurs.

try:
     string = input()
     print(string)
except EOFError:
     print("no data provided to input function")

If you are a beginner, this snippet won't be needed as you start in Python. Just for a friendly tip, you should name variables relevant to their purpose (ex: naming the sum of two numbers sum1 instead of integer). Naming it after its datatype is okay for very short-term uses but AFAIK is not conventional in any coding language.

Official Documentation: EOFError

Upvotes: 6

Kaia
Kaia

Reputation: 893

TL;DR: You are likely running your script via a method that either doesn't support input, or requires you to predefine the entire input before running the program.

(Some of this will be simplified--if you're curious, you can read more about how file descriptors are handled on a python, C, or operating system level.)

Under the hood, Python programs (and most processes on your computer) have 3 standard streams for input and output. For input, this is called stdin and you can directly access it as sys.stdin. These behave like files: you can write, read, and close them just like you would a open('myFile.txt', 'r') file. They might be real files on your computer, or they might be other things that can be accessed the same way, namely pipes. Pipes are a way for two processes to communicate. One end is a writable file, and the other end is a readable file that contains whatever has been written to the other end.

The program that calls a python script decides how to set up that script's stdin/stdout streams. If you run your code through a terminal/command line, by default the terminal will create a pipe for stdin. When you hit enter, it will send anything you typed to the python script's stdin. If you run your script in an IDE or text editor it will likely do the same.

In this case, the input() will block, preventing the script from executing indefinitely until it gets another line of input. That's why execution waits at an input() until you type something and hit enter.

However, in some situations, you might be running a python script with a different stdin. For instance:

  • An auto-grading system that tries your program with different inputs.
  • An editor that doesn't support passing stdin to your program. (Some online editors, and at one point, Sublime Text)
  • You decide to redirect stdin, e.g. with python myScript.py < input.txt
  • You signal that you're done providing input, usually with CTRL+D (Linux) or CTRL+Z (Windows), which sets stdin to an EOF-state.

In these cases, once everything's been read out of stdin, you'll get this EOFError: EOF when reading a line.

You can try this out using Python's subprocess module:

# parent_script.py
import subprocess

print('Running with predefined input:')
subprocess.run(["python", "child_script.py"], input=b"first line of input\nsecond line of input\n", check=False)
print('\nRunning with interactive input:')
subprocess.run(["python", "child_script.py"], check=False)

# child_script.py
while True:
    print("stdin: " + input(""), flush=True)

Output when we run parent_script.py:

stdin: first line of input
stdin: second line of input
EOFError: EOF when reading a line

Running with interactive input:
<User types "hello!">
stdin: hello!
<User types CTRL+D>
EOFError: EOF when reading a line

We can see that if we call child_script.py with a predefined input, it results in a EOFError once all the lines of that input are exhausted.

More questions with related answers:

Upvotes: 1

Radoslaw Lesniak
Radoslaw Lesniak

Reputation: 1

Check what you get from readline() and if it's empty avoid EOF error by checking content:

  file1 = open("some_file", "r")
  pack_a = 1
  while pack_a:
      pack_a = file1.readline()
      pack_a = pack_a .replace(',', ' ').split()
      if pack_a != []:
     # last element will be iqual to []

Upvotes: -3

Related Questions