[Solved]: IOError in Python

Python IOError is subclass of EnvironmentError. Python IOError is occurred when an input/output operation like open() file, or a method or a simple print statement is failed due to IO reason like “Disk full” or “File not found”

See Also:

Example of Python IOError

In the below code, the xyz.txt file is not exist because of that the program will throw IOError exception. This Python IOError handle in the program through try-except block.

import sys
def readFile():
    try:
        f = open ( "xyz.txt", 'r' )
    except IOError as e:
        print (e)
readFile()

Output

[Errno 2] No such file or directory: 'xyz.txt'

This IOError exception message will print only when exception occurred.

Conclusion

Python IOError is a result of incorrect file name or file path. IOError occurred in multiple cases and these conditions can be handled using try except code block. Implementation way to handle IOError by try-except can check by above example.

Related Posts

Your Feedback Motivate Us

If our FacingIssuesOnIT Experts solutions guide you to resolve your issues and improve your knowledge. Please share your comments, like and subscribe to get notifications for our posts.

Happy Learning !!!