Technical skill practice

EOF Error in Python: Reproduce Missing Input Safely

Python input raises EOFError when it reaches the end of input without reading a line. A blank line is different: it can return an empty string. Incomplete Python source is a parsing problem that can raise SyntaxError, even when a message mentions an unexpected end.

Start with a small, explicit task

Replace stdin only within an isolated script using StringIO fixtures. First provide a blank line, then no input at all. Finally compile a separate unfinished source string. No terminal interaction, user input or real file is needed to inspect the two exception types.

Run the complete example

This original example was checked with Python 3.12.14. Save it as python_eof_error.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.

Expected output

blank line: ''

EOFError SyntaxError

import io
import sys
original = sys.stdin
try:
    sys.stdin = io.StringIO("\n")
    blank = input()
    assert blank == ""
    sys.stdin = io.StringIO("")
    try:
        input()
    except EOFError:
        missing = "EOFError"
    else:
        raise AssertionError("expected EOFError")
finally:
    sys.stdin = original
try:
    compile("items = [1,\n", "<unfinished>", "exec")
except SyntaxError:
    unfinished = "SyntaxError"
else:
    raise AssertionError("expected SyntaxError")
assert sys.stdin is original
print("blank line:", repr(blank))
print(missing, unfinished)

Explain the result

The newline fixture contains a complete empty line, so input returns an empty string after removing the line ending. The zero-character stream has no line to read and raises EOFError. Treating both as simply false input would lose the distinction between a supplied blank value and an unavailable input source.

The unfinished list expression fails during compilation, producing SyntaxError rather than EOFError. The script checks types rather than exact diagnostic messages, whose wording can vary. Restoring stdin in finally prevents the local fixture from leaking into later operations in this isolated process.

Avoid these mistakes

Do not blindly retry input after permanent EOF: the same exhausted stream may remain exhausted. Decide whether EOF is a normal end-of-data signal or a missing-required-input error for the program. Fix unmatched delimiters or incomplete source as syntax problems instead of adding an input exception handler.

Questions, answered

Is pressing Enter on a blank line EOF?

In the checked fixture it is a line containing a newline, so input returns an empty string rather than raising EOFError.

Will catching EOFError fix incomplete code?

No. The unfinished source fixture raises SyntaxError before it could execute.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.