End of file reached
Production Risk
Common in automation pipelines; always handle EOFError when reading from stdin.
Raised when input() reaches the end of the input stream (EOF) without reading any data. Common in scripts that read from stdin when there is no more input.
- 1input() called in a script piped from a file or process that has ended
- 2Interactive session closed while waiting for input()
- 3Reading from stdin in a Docker container or CI environment with no TTY
Script calls input() when stdin is a closed pipe.
echo "" | python3 -c "name = input('Name: ')"expected output
Traceback (most recent call last): File "<string>", line 1, in <module> EOFError: EOF when reading a line
Fix 1
Wrap input() in a try/except
WHEN Script reads from stdin that may be closed
try:
line = input("Name: ")
except EOFError:
line = "" # or use a default, or exitWhy this works
Catching EOFError lets the script handle piped or redirected input gracefully.
Fix 2
Use sys.stdin.read() for batch input
WHEN Reading all stdin at once
import sys data = sys.stdin.read() # returns "" at EOF, no exception
Why this works
sys.stdin.read() returns an empty string at EOF rather than raising EOFError.
# echo "" | python3 -c "name = input()" # EOFError: EOF when reading a line
try:
line = input("> ")
except EOFError:
line = "" # no input availableimport sys data = sys.stdin.read() # returns "" at EOF without raising
Python Docs — Built-in Exceptions
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev