Python Eof Error When Reading a Line
Introduction to Python EOFError
EOFError in python is one of the exception treatment errors, and it is raised in scenarios such as interruption of the input() part in both python version 2.7 and python version 3.6 and other versions after version 3.vi or when the input() role reaches the unexpected end of the file in python version 2.seven, that is the functions practise not read any date before the stop of input is encountered. And the methods such as the read() method must render a cord 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:
ane. BaseException class is the base of operations course of Exception class which in turn inherits the EOFError grade.
2. EOFError is not an mistake technically, simply it is an exception. When the in-built functions such as input() role or read() function returns a string that is empty without reading whatever data, and then the EOFError exception is raised.
3. This exception is raised when our program is trying to obtain something and practise modifications to it, but when information technology fails to read any information and returns a string that is empty, EOFError exception is raised.
Examples to Implement Python EOFError
Below are the example of Python EOFError:
Case #1
Python program to demonstrate EOFError with an fault message in the program.
Code:
#EOFError program
#effort and except blocks are used to catch the exception
endeavour:
while True:
#input is assigned to a string variable check
cheque = raw_input('The input provided by the user is being read which is:')
#the data assigned to the string variable is read
print 'READ:', check
#EOFError exception is caught and the advisable bulletin is displayed
except EOFError every bit x:
print 10
Output:
Explanation: In the in a higher place program, try and except blocks are used to catch the exception. A while block is used within a try block, which is evaluated to true, and as long equally the condition is truthful, the data provided by the user is read, and it is displayed using a print statement, and if the data cannot be read with an empty string beingness returned, and then the except cake raises an exception with the message which is shown in the output.
Example #2
Python programme to demonstrate EOFError with an error message in the program.
Code:
#EOFError program
#try and except blocks are used to catch the exception
effort:
while True:
#input is assigned to a cord variable check
check = raw_input('The input provided by the user is being read which is:')
#the information 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 every bit ten:
impress x
Output:
Caption: In the above programme, effort and except blocks are used to catch the exception. A while block is used within a try block, which is evaluated to truthful, and as long as the condition is true, the information provided by the user is read and information technology is displayed using impress statement, and if the data cannot be read with an empty string beingness returned, then the except block raises an exception with the message which is shown in the output.
Steps to Avert EOFError in Python
If End of file Error or EOFError happens without reading whatsoever data using the input() function, an EOFError exception volition exist raised. In club to avoid this exception being raised, nosotros can try the following options which are:
- Earlier sending the End of File exception, try to input something like CTRL + Z or CTRL + D or an empty cord which the below example tin demonstrate:
Lawmaking:
#effort and except blocks are used to catch the exception
effort:
data = raw_input ("Do you want to continue?: ")
except EOFError:
print ("Error: No input or End Of File is reached!")
data = ""
print data
Output:
Caption: In the above programme, endeavour and except blocks are used to avoid the EOFError exception by using an empty string which volition not print the Terminate Of File fault message and rather print the custom message provided past is which is shown in the program and the same is printed in the output equally well. The output of the program is shown in the snapshot above.
If the EOFError exception must be candy, try and take hold of block tin be used.
Conclusion
In this tutorial, we understand the concept of EOFError in Python through definition, the syntax of EOFError in Python, working of EOFError in Python through programming examples and their outputs and the steps to avoid EOFError in Python.
Recommended Manufactures
This is a guide to Python EOFError. Here nosotros hash out the Introduction and Working of Python EOFError forth with Examples and Code Implementation. You can also go through our other suggested articles to learn more –
- Introduction to Python Range Office
- Superlative 7 Methods of Python Set Role
- Python Zip Function | Examples
- Guide to Examples of Python Turtle
Source: https://www.educba.com/python-eoferror/
Belum ada Komentar untuk "Python Eof Error When Reading a Line"
Posting Komentar