Skip to content

How to Fix Python EOFError: Ran Out of Input

DodaTech 2 min read

In this tutorial, you'll learn about How to Fix Python EOFError: Ran Out of Input. We cover key concepts, practical examples, and best practices.

The Problem

Python raises EOFError: Ran out of input when input() reaches end-of-file with no data, or when pickle.load() tries to read an empty or corrupted file.

Quick Fix

Step 1: Reproduce the error with empty input

Pipe empty data to a script that calls input():

echo -n "" | python3 -c "name = input('Enter name: ')"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
EOFError: EOF when reading a line

Step 2: Catch EOFError with try-except

Wrap input() in a try-except to provide a fallback:

python3 -c "
try:
    name = input('Enter name: ')
except EOFError:
    name = 'default_user'
print(f'Hello, {name}')
" < /dev/null
Hello, default_user

Step 3: Fix unpickling EOFError

An empty pickle file causes EOFError during load:

import pickle

data = {}
try:
    with open('data.pkl', 'rb') as f:
        data = pickle.load(f)
except (EOFError, FileNotFoundError):
    data = {}
    print('No existing data, starting fresh')

Step 4: Check for empty files before reading

Use os.path.getsize() to skip empty files:

python3 -c "
import os, pickle
path = 'data.pkl'
if os.path.exists(path) and os.path.getsize(path) > 0:
    with open(path, 'rb') as f:
        data = pickle.load(f)
else:
    data = {}
    print('File is empty or missing, using empty dict')
"

Alternative Solutions

Use input with a sentinel default

Wrap input calls in a helper that returns a default on EOF:

def safe_input(prompt, default=''):
    try:
        return input(prompt)
    except EOFError:
        return default

Check JSON data before unpickling

For JSON files, validate content before parsing:

import json
import os
path = 'data.json'
if os.path.exists(path) and os.path.getsize(path) > 0:
    with open(path) as f:
        try:
            data = json.load(f)
        except json.JSONDecodeError:
            data = {}

Common Mistakes to Avoid

Not handling empty files when unpickling. A file that was created but never written to is empty. Trying pickle.load() on it raises EOFError. Check file size first.

Using input() in automated scripts without a fallback. When stdin is closed or redirected from /dev/null, input() raises EOFError. Always wrap in try-except for production scripts.

Assuming a file has valid data because it exists. A corrupted or truncated file passes existence checks but fails on read. Use try-except around all file reads.

Pro Tips

Use sys.stdin.isatty() to check for interactive input. Before calling input(), check if stdin is a terminal: if sys.stdin.isatty(): name = input("Name: ") else: name = "default".

Use pickle.DEFAULT_PROTOCOL when dumping. Use the highest protocol for efficient serialization, and always check the file was written before reading.

Use json.load with a try-except for config files. JSON configuration files are often hand-edited and can be empty or malformed. Always handle both EOFError and JSONDecodeError.

Prevention

  • Wrap input() in a try-except when running in non-interactive environments.
  • Check file size before unpickling to avoid EOFError on empty files.
  • Use pickle.load() inside a try-except that also catches FileNotFoundError.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro