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().
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
How to fix it
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.
Fixed code
try:
name = input("Enter your name: ")
print(f"Hello, {name}")
except EOFError:
print("No input provided, using default")
name = "Guest"Related errors
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