Python EOFError – Tutorial with Examples

Python EOFError - Tutorial with Examples

In Python, the abbreviations EOF stands for “End Of File”. It is an exception in Python that gets raised when there is no input from the user or from the system by default.

The reason that an EOFError exception is raised is that any input() function call expects input data to be enter by the user. The input() function will continue to prompt for input until a new line is entered. But if a user does not enter any input and press the Ctrl+d (EOF) key combination then Python recognizes an End Of File as the input.

Examples:

Example 1: Handling EOFError Exception

The following example shows how to handle the “EOFError” exception. In this example, we will take input from the user until the user input is an integer value.

try:
    while True:
        num = int(input("Enter a number: "))
        print(num)
except EOFError:
    print("\nEnd of input reached!")

Output:

Enter a number: 5
5
Enter a number: 10
10
Enter a number: abc
Traceback (most recent call last):
  File "EOFError_Example.py", line 3, in <module>
    num = int(input("Enter a number: "))
ValueError: invalid literal for int() with base 10: 'abc'

In the above example, we have a while loop that is running until an EOFError is raised. Inside the while loop, we are getting an input from the user and converting it to an integer using int() built-in function. If the user enters a non-integer input, the ValueError exception will be raised, which will terminate the program by default.

We are using the try-except block to handle the EOFError exception. When the user input raises the EOFError exception, the program will terminate and print the End Of File reached message.

Example 2: Using CTRL+D (EOF) to raise EOFError

The following example demonstrates that by using the CTRL-d key combination (which represents the E0F), we can raise the EOFError exception.

try:
    while True:
        input('Enter a character: ')
except EOFError:
    print('\nEnd of input reached.')

Output:

Enter a character: a
Enter a character: b
Enter a character: c
Enter a character: d
Enter a character: 
End of input reached.

Example 3: Handling Minimization of EOFError Exception

Let’s say you got an EOFError and you want to minimize it further.

print("Enter a number")
try:
    line = input("=> ")
    num = int(line)
except EOFError:
    print("Why did you do an EOF on me?")
except ValueError:
    print("You must enter an integer")
else:
    print(f"You have entered {num}")

Output:

Enter a number
=> 25
You have entered 25

In the above example, we are trying to execute a do-while loop by using While True. Inside the while loop, we are asking for user input by using the input() function at line 3. Then we are trying to convert the input value to an integer using the int() built-in function at line 4.

If the user entered input value is a non-integer value, it will raise the ValueError exception that we will handle and print the “You must enter an integer” message on the console. But if we use the Ctrl+d key combination during the input process, it will raise the EOFError exception, which we will handle and print the “Why did you do an EOF on me?” message.

If there is no exception, then we have a block of code that will execute and display the User-entered message value along with the message “You have entered input_value_” on the console.

Conclusion

EOFError exception generally occurs when the user provides no input, provides input and then uses Ctrl+d key combination. These type of exception can be handled by using try-except block.

Leave a Reply

Your email address will not be published. Required fields are marked *