Eoferror: Eof When Reading a Line Python

Python EOFError

Introduction to Python EOFError

EOFError in python is i of the exception handling errors, and information technology is raised in scenarios such as interruption of the input() office in both python version two.7 and python version iii.6 and other versions after version 3.6 or when the input() role reaches the unexpected cease of the file in python version 2.7, that is the functions do not read whatsoever engagement before the end of input is encountered. And the methods such equally the read() method must return a string that is empty when the end of the file is encountered, and this EOFError in python is inherited from the Exception class, which in plow is inherited from BaseException grade.

Syntax:

EOFError: EOF when reading a line

Working of EOFError in Python

Below are the working of Python EOFError:

1. BaseException class is the base class of Exception course which in turn inherits the EOFError course.

2. EOFError is not an fault technically, just it is an exception. When the in-built functions such every bit input() part or read() function returns a string that is empty without reading any data, so the EOFError exception is raised.

three. This exception is raised when our program is trying to obtain something and do modifications to it, but when it fails to read whatever information and returns a string that is empty, EOFError exception is raised.

Examples to Implement Python EOFError

Below are the instance of Python EOFError:

Example #one

Python plan to demonstrate EOFError with an error message in the program.

Code:

#EOFError programme
#effort and except blocks are used to catch the exception
effort:
while True:
#input is assigned to a string variable bank check
check = raw_input('The input provided by the user is being read which is:')
#the data assigned to the cord variable is read
impress 'READ:', check
#EOFError exception is defenseless and the appropriate bulletin is displayed
except EOFError as x:
print x

Output:

Python EOFError Example 1

Explanation: In the above program, endeavour and except blocks are used to take hold of the exception. A while block is used within a try cake, which is evaluated to true, and as long equally the status is true, the information provided past the user is read, and it is displayed using a print argument, and if the data cannot be read with an empty string being returned, and so the except block raises an exception with the bulletin which is shown in the output.

Example #2

Python program to demonstrate EOFError with an error bulletin in the plan.

Code:

#EOFError programme
#try and except blocks are used to take hold of the exception
effort:
while True:
#input is assigned to a cord variable check
check = raw_input('The input provided by the user is existence read which is:')
#the data assigned to the string variable is read
print 'How-do-you-do', check
#EOFError exception is caught and the appropriate message is displayed
except EOFError as x:
print 10

Output:

Python EOFError Example 2

Caption: In the higher up programme, try and except blocks are used to catch the exception. A while block is used within a endeavour cake, which is evaluated to true, and as long every bit the condition is truthful, the data provided by the user is read and it is displayed using print statement, and if the information cannot be read with an empty string being returned, and then the except block raises an exception with the message which is shown in the output.

Steps to Avoid EOFError in Python

If End of file Error or EOFError happens without reading any data using the input() function, an EOFError exception volition exist raised. In guild to avoid this exception being raised, we can try the following options which are:

  • Before sending the Cease of File exception, endeavour to input something like CTRL + Z or CTRL + D or an empty string which the below case can demonstrate:

Code:

0 Response to "Eoferror: Eof When Reading a Line Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel