Eof when reading a line python что это

от admin

EOFError: EOF when reading a line


image
So as we can see in the pictures above, despite having produced the expected output, our test case fails due to a runtime error EOFError i.e., End of File Error. Let’s understand what is EOF and how to tackle it.

What is EOFError

In Python, an EOFError is an exception that gets raised when functions such as input() or raw_input() in case of python2 return end-of-file (EOF) without reading any input.

When can we expect EOFError

We can expect EOF in few cases which have to deal with input() / raw_input() such as:

image

Interrupt code in execution using ctrl+d when an input statement is being executed as shown below

Another possible case to encounter EOF is, when we want to take some number of inputs from user i.e., we do not know the exact number of inputs; hence we run an infinite loop for accepting inputs as below, and get a Traceback Error at the very last iteration of our infinite loop because user does not give any input at that iteration

Exit fullscreen mode

The code above gives EOFError because the input statement inside while loop raises an exception at last iteration

Do not worry if you don’t understand the code or don’t get context of the code, its just a solution of one of the problem statements on HackerRank 30 days of code challenge which you might want to check
The important part here is, that I used an infinite while loop to accept input which gave me a runtime error.

How to tackle EOFError

We can catch EOFError as any other error, by using try-except blocks as shown below :

Exit fullscreen mode

You might want to do something else instead of just printing «EOF» on the console such as:

Exit fullscreen mode

In the code above, python exits out of the loop if it encounters EOFError and we pass our test case, the problem due to which this discussion began.
alt=»image» width=»880″ height=»433″ />

Hope this is helpful
If you know any other cases where we can expect EOFError, you might consider commenting them below.

Dec 27, 2017 9:30:42 PM | Python Exception Handling — EOFError

A close look at the EOFError in Python, including a functional code sample showing how to handle user input in both Python 2 and Python 3.

Share

Moving along through our in-depth Python Exception Handling series, today we’ll be going over the EOFError. The EOFError is raised by Python in a handful of specific scenarios: When the input() function is interrupted in both Python 2.7 and Python 3.6+, or when input() reaches the end of a file unexpectedly in Python 2.7.

Throughout this article we’ll examine the EOFError by seeing where it resides in the overall Python Exception Class Hierarchy. We’ll also look at some fully functional code examples that illustrate how the different major versions of Python handle user input, and how improper use of this functionality can sometimes produce EOFErrors , so let’s get to it!

The Technical Rundown

All Python exceptions inherit from the BaseException class, or extend from an inherited class therein. The full exception hierarchy of this error is:

Похожие статьи