IOPython Error

EOFError

EOFError: EOF when reading a line

Traceback

terminal
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    process(line)
EOFError: EOF when reading a line

What causes this error

A read operation (input(), pickle.load(), etc.) reached the end of the input stream without reading the expected data. The input was exhausted or the connection was closed.

How to fix it

Wrap input() calls in try/except EOFError. Use sys.stdin for more control over input handling. Check file/stream state before reading. Handle the end of piped input gracefully.

Code that causes this error

Broken
while True:
    line = input("Enter command: ")
    process(line)

Fixed code

Fixed
while True:
    try:
        line = input("Enter command: ")
    except EOFError:
        print("\nEnd of input")
        break
    process(line)

About EOFError

EOFError is raised when a built-in function like `input()` or `raw_input()` (Python 2) hits end-of-file without reading any data. This happens when reading from a pipe or redirected input that has been exhausted, when a network connection closes during a read operation that expects more data, or when interactively pressing Ctrl+D (Unix) or Ctrl+Z+Enter (Windows) at an input prompt. EOFError is also raised by `pickle.load()` when the pickle stream ends unexpectedly, and by various binary parsers when they encounter a premature end of data.

The typical fix is to wrap `input()` in a try/except EOFError block, use `sys.stdin.read()` which returns an empty string at EOF instead of raising, or check for the end condition before attempting to read.

Common scenarios

1

Opening files with incorrect or relative paths

2

Reading files that have been moved, renamed, or deleted

3

Writing to directories instead of files or vice versa

4

Working with file streams that have been closed prematurely

Related errors