EOFError: What It Means and How to Fix It
Error reference
EOFError
Language
Severity
LowWhen it happens
EOFError is raised when the built-in input() function hits the end of a file or stream without reading any data.
Potential fixes
Wrap input() calls in a try/except EOFError block to handle the case gracefully. When processing pipelines, check whether stdin is a terminal with sys.stdin.isatty() before calling input(). Provide default values for non-interactive contexts.
Related errors
Deep dive
EOFError is raised when the built-in input() function hits the end of a file or stream without reading any data. This commonly happens in scripts that read from stdin when the input stream is closed or redirected from an empty source.
Why it happens
- •The user presses Ctrl+D (Unix) or Ctrl+Z (Windows) to send EOF during input().
- •The script reads from stdin but is run with an empty or exhausted pipe.
- •Automated testing frameworks that don't provide stdin to the process.
- •Reading past the end of a file-like object passed to input().
Potential fixes
Wrap input() calls in a try/except EOFError block to handle the case gracefully. When processing pipelines, check whether stdin is a terminal with sys.stdin.isatty() before calling input(). Provide default values for non-interactive contexts.
Examples
Code that triggers the error
# Run this and immediately send EOF (Ctrl+D)
name = input("Enter your name: ")
print(f"Hello, {name}")Error output
Enter your name: EOFError: EOF when reading a line
Fixed code
try:
name = input("Enter your name: ")
print(f"Hello, {name}")
except EOFError:
print("No input provided, using default")
name = "Guest"Practice in English
How would you explain an EOFError to a fellow dev? Choose the right phrase:
"I hit an error...
Ready to practice your English at work?
Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.
Try Lingua-e for free