Last updated: Apr 11, 2024
Reading time·5 min
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.
Ctrl
+ C
, Ctrl
+ D
or Ctrl
+ Z
when you get prompted for user input as that would cancel the prompt without collecting any input.Suppose you have the following main.py
file.
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.
python main.py # Or with python3 (macOS and Linux) python3 main.py # Or using py alias (Windows) py main.py
You can then type the input value and press Enter
.
The color
variable will get set to the value you typed.
try/except
statement to handle the errorOne way to handle the error is to use a try/except
statement.
try: color = input('Enter a color: ') print(f'The color is: {color}') except EOFError as e: print(end='') print('\nEOFError occurred')
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.
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)
If an error occurs we set the color
variable to the string default color
.
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.
color = input('Enter a color: ') print(f'The color is: {color}') letter = input('Enter a letter: ') print(f'The letter is: {letter}')
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.
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
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.
One way to get around this is to split the supplied string and use a single
input()
call.
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:
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
Or you can use the python main.py
command and supply a space-separated string,
e.g. green b
.
python main.py # Or with python3 (macOS and Linux) python3 main.py # Or using py alias (Windows) py main.py
The str.split() method splits the string into a list of substrings using a delimiter.
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.
If you need to take multiple
integers from user input, use the
map()
function.
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}')
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.
while
loopIf you
prompt the user for input in a for
or a while
loop,
you need to handle the EOFError
exception.
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)
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.
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.
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.
You can learn more about the related topics by checking out the following tutorials:
__main__
module in Path