EOFError: EOF when reading a line in Python [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
5 min

banner

# Table of Contents

  1. EOFError: EOF when reading a line in Python
  2. Using a try/except statement to handle the error
  3. Make sure a value for each input() call is supplied
  4. Split the string and use a single input() call
  5. Splitting the input string and converting the values to integers
  6. Handing the exception when prompting for input in a while loop

# EOFError: EOF when reading a line in Python [Solved]

The Python error "EOFError: EOF when reading a line" occurs when you use the input() function to prompt for user input but don't enter a value.

To solve the error, use a try/except block to handle the EOFError exception or supply a value for each input() call.

Make sure you aren't pressing Ctrl + C, Ctrl + D or Ctrl + Z when you get prompted for user input as that would cancel the prompt without collecting any input.

eof error eof when reading line

Suppose you have the following main.py file.

main.py
color = input('Enter a color: ') print(f'The color is: {color}')

You can run the script by issuing the python main.py command from your terminal.

Note that your terminal has to be located in the same directory as your python main.py file.

shell
python main.py # Or with python3 (macOS and Linux) python3 main.py # Or using py alias (Windows) py main.py

make sure to supply input value

You can then type the input value and press Enter.

The color variable will get set to the value you typed.

# Using a try/except statement to handle the error

One way to handle the error is to use a try/except statement.

main.py
try: color = input('Enter a color: ') print(f'The color is: {color}') except EOFError as e: print(end='') print('\nEOFError occurred')

handle eof error using try except

We call the input() function in the try block and if an EOFError exception is raised, the except block runs.

You can set the color variable to a default value in the except block or handle the exception in any other way that suits your use case.

main.py
try: color = input('Enter a color: ') print(f'The color is: {color}') except EOFError as e: color = 'default color' print(end='') print('\nEOFError occurred') print(color)

supply default value if eoferror occurs

If an error occurs we set the color variable to the string default color.

# Make sure a value for each input() call is supplied

The error commonly occurs when you have multiple input() calls but only supply a value for some.

Suppose you have the following main.py script.

main.py
color = input('Enter a color: ') print(f'The color is: {color}') letter = input('Enter a letter: ') print(f'The letter is: {letter}')

supply value for all input calls

Notice that I got prompted for a value twice because the file contains 2 calls to the input() function.

If you only provide a value for the first input() call, the error is raised.

Here is an example of only providing a value for the first input() call.

shell
echo "green b" | python main.py # Or with python3 echo "green b" | python3 main.py # Or using py alias (Windows) echo "green b" | py main.py

eof when reading a line

We used the echo command and piped a string to the python main.py command.

However, the string only supplies a value for the first input() call.

No value is provided for the second input() call which caused the error.

# Split the string and use a single input() call

One way to get around this is to split the supplied string and use a single input() call.

main.py
value = input( 'Enter space-separated color & letter: ' ) color, letter = value.split() print(f'\nThe color is {color}') print(f'\nThe letter is {letter}')

You can run your script as follows:

shell
echo "green b" | python main.py # Or with python3 echo "green b" | python3 main.py # Or using py alias (Windows) echo "green b" | py main.py

run script by piping echo

Or you can use the python main.py command and supply a space-separated string, e.g. green b.

shell
python main.py # Or with python3 (macOS and Linux) python3 main.py # Or using py alias (Windows) py main.py

supply space separated string when calling input

The str.split() method splits the string into a list of substrings using a delimiter.

main.py
value = input( 'Enter space-separated color & letter: ' ) color, letter = value.split() print(f'\nThe color is {color}') print(f'\nThe letter is {letter}')

When you call the method without a parameter, it splits on one or more whitespace characters.

# Splitting the input string and converting the values to integers

If you need to take multiple integers from user input, use the map() function.

main.py
value = input( 'Enter 2 space-separated numbers: ' ) num1, num2 = map(int, value.split()) print(f'\nThe first number is {num1}') print(f'\nThe second number is {num2}')

enter multiple space separated numeric inputs

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

We passed the int() class to the map() function, so it gets called with each number from the list of strings.

# Handing the exception when prompting for input in a while loop

If you prompt the user for input in a for or a while loop, you need to handle the EOFError exception.

main.py
a_list = [] while True: letter = '' try: letter = input('Enter a letter: ') if letter: a_list.append(letter) except EOFError as e: break if letter == '': break print(a_list)

handle eof error exception in while loop

We used a while True loop, so the user gets prompted for input until a break statement runs.

On each iteration, we prompt the user for input and if they entered a value, it gets appended to the list.

If the user presses Ctrl + D or doesn't supply any input, the except block runs.

If the user presses Enter without supplying a value, the if statement runs and breaks out of the loop.

# Conclusion

Make sure to supply a value for each input() call to solve the "EOFError: EOF when reading a line" error.

The error most commonly occurs when you only supply a value for some of the input() calls.

If you need to handle the error, use a try/except statement.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.