EOFError

input() asked for a line and stdin had nothing left — common when a script that expects a keyboard runs in CI, Docker, an online IDE or with piped input.

InheritsBaseException›Exception›EOFError
Runtime exceptionPython 3 (all)Live demo
EOFError(*args)
Raised by
input() at end of stdin; pickle.load()/loads() on empty or exhausted data; marshal.loads(b"")
Message
EOF when reading a line · Ran out of input
Quick fix
try/except EOFError around input(); check the data source is not empty
Watch out
file.read()/readline() return "" at EOF — they never raise it

Demo

Live evaluation
Feed input() from a string instead of the keyboard, then ask for n lines. (sys.stdin is swapped for an io.StringIO and restored afterwards.)
Try:
Inputs
lineslist[str]comma-separated lines on stdin
ninthow many input() calls
Code
import io, sys
sys.stdin = io.StringIO('\n'.join(['alice', '42']))
try:
    answers = [input() for _ in range(2)]
finally:
    sys.stdin = sys.__stdin__
answers
Result
['alice', '42']

An empty line is not EOF: input() returns '' for it and keeps going. EOFError comes only when stdin has nothing at all left — the third input() on two lines, or the first input() on empty stdin. That is exactly what happens when a script runs with no keyboard attached: stdin is empty (or closed) from the start.

Constructor

NameTypeRequiredDescription
*argsobjectnoThe message; input() passes "EOF when reading a line", pickle "Ran out of input".

Attributes

AttributeTypeMeaning
argstupleThe message tuple, e.g. ('EOF when reading a line',).

Common patterns

Prompt that survives no-terminal runs
Fall back to a default when stdin is empty or closed (CI, cron, Docker without -it).
try:
    name = input('Name: ')
except EOFError:
    name = 'anonymous'
Read all of stdin without EOFError
Iterating sys.stdin (or sys.stdin.read()) just stops at EOF — no exception to handle.
import sys
for line in sys.stdin:
    process(line.rstrip('\n'))
Load every pickled record in a file
pickle.load raises EOFError after the last object — the standard end-of-stream signal.
records = []
with open('data.pkl', 'rb') as f:
    while True:
        try:
            records.append(pickle.load(f))
        except EOFError:
            break

Examples

1. input() on empty stdin
import io, sys sys.stdin = io.StringIO('') try: input() finally: sys.stdin = sys.__stdin__
Returns
EOFError: EOF when reading a line
2. An empty line is not EOF
import io, sys sys.stdin = io.StringIO('\n') try: line = input() finally: sys.stdin = sys.__stdin__ line
Returns
''
3. readline() returns "" instead
import io f = io.StringIO('only line') f.readline(), f.readline()
Returns
('only line', '')
4. pickle.loads on empty bytes
import pickle pickle.loads(b'')
Returns
EOFError: Ran out of input
5. pickle.load on an empty file
import pickle open('cache.pkl', 'wb').close() with open('cache.pkl', 'rb') as f: data = pickle.load(f)
Returns
EOFError: Ran out of input
6. One load too many
import io, pickle buf = io.BytesIO() pickle.dump('first', buf) buf.seek(0) [pickle.load(buf), pickle.load(buf)]
Returns
EOFError: Ran out of input
7. marshal too
import marshal marshal.loads(b'')
Returns
EOFError: EOF read where object expected

Pitfalls

1. Expecting input() to return "" at the end
Files return "" at EOF, but input() raises. A while-loop that waits for "" never gets it.
while line != ''
import io, sys
sys.stdin = io.StringIO('a\nb')
lines = []
try:
    line = input()
    while line != '':
        lines.append(line)
        line = input()
finally:
    sys.stdin = sys.__stdin__
lines
EOFError: EOF when reading a line
except EOFError
import io, sys
sys.stdin = io.StringIO('a\nb')
lines = []
try:
    while True:
        lines.append(input())
except EOFError:
    pass
finally:
    sys.stdin = sys.__stdin__
lines
['a', 'b']
2. Truncated pickle is not EOFError
Only completely empty data gives EOFError. Data cut off in the middle raises UnpicklingError — catch both when reading untrusted or partially written caches.
except EOFError
import pickle
data = pickle.dumps([1, 2, 3])[:5]
try:
    obj = pickle.loads(data)
except EOFError:
    obj = []
obj
_pickle.UnpicklingError: pickle data was truncated
Both errors
import pickle
data = pickle.dumps([1, 2, 3])[:5]
try:
    obj = pickle.loads(data)
except (EOFError, pickle.UnpicklingError):
    obj = []
obj
[]

When to use

Use it
  • Detecting the end of interactive or piped input to input()
  • Ending a loop over consecutive pickle.load() calls
  • Raising it from your own reader when a stream ends before a complete record
Reach for something else
  • Reading files line by line → iterate the file; it stops at EOF by itself
  • Scripts meant for automation → take arguments (argparse) or read sys.stdin instead of prompting

Notes

Keyboard EOF
Ctrl-D on Linux/macOS, Ctrl-Z then Enter on Windows — input() raises EOFError
Not Ctrl-C
Ctrl-C raises KeyboardInterrupt, not EOFError
Closed stdin
If sys.stdin is None, input() raises RuntimeError: lost sys.stdin instead

FAQ

input() found stdin already at end-of-file. Common causes: the script runs where there is no keyboard (CI jobs, cron, Docker without -it, some online IDEs and editor 'Run' buttons that do not attach a console), input is piped from a file with fewer lines than the script asks for, or the user pressed Ctrl-D (Ctrl-Z Enter on Windows). Either provide the input (echo 'alice' | python script.py, docker run -it), or catch EOFError and use a default.