Python Error Handling Ioerror
Contents |
you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions. 8.1. Syntax Errors¶ Syntax errors, also known as parsing errors, are perhaps the most common kind
Python Exception Class
of complaint you get while you are still learning Python: >>> while True print python exception message 'Hello world' File "
Python Print Exception
example, the error is detected at the keyword print, since a colon (':') is missing before it. File name and line number are printed so you know where to look in case the input came from a script. 8.2. Exceptions¶ Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions
Python Custom Exception
and are not unconditionally fatal: you will soon learn how to handle them in Python programs. Most exceptions are not handled by programs, however, and result in error messages as shown here: >>> 10 * (1/0) Traceback (most recent call last): File "
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more syntax for generic except clause in python about Stack Overflow the company Business Learn more about hiring developers or posting
Python Try Without Except
ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack python try except else Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Python Exception handling up vote 15 down vote favorite 5 C https://docs.python.org/2.7/tutorial/errors.html has perror and errno, which print and store the last error encountered. This is convenient when doing file io as I do not have to fstat() every file that fails as an argument to fopen() to present the user with a reason why the call failed. I was wondering what is the proper way to grab errno when gracefully handling the IOError exception in python? In [1]: fp = http://stackoverflow.com/questions/1134607/python-exception-handling open("/notthere") --------------------------------------------------------------------------- IOError Traceback (most recent call last) /home/mugen/ in () IOError: [Errno 2] No such file or directory: '/notthere' In [2]: fp = open("test/testfile") --------------------------------------------------------------------------- IOError Traceback (most recent call last) /home/mugen/ in () IOError: [Errno 13] Permission denied: 'test/testfile' In [5]: try: ...: fp = open("nothere") ...: except IOError: ...: print "This failed for some reason..." ...: ...: This failed for some reason... python exception errno ioerror share|improve this question asked Jul 15 '09 at 23:15 jr0d 340249 add a comment| 3 Answers 3 active oldest votes up vote 18 down vote accepted The Exception has a errno attribute: try: fp = open("nother") except IOError, e: print e.errno print e share|improve this answer answered Jul 15 '09 at 23:18 stefanw 7,77922531 add a comment| up vote 22 down vote Here's how you can do it. Also see the errno module and os.strerror function for some utilities. import os, errno try: f = open('asdfasdf', 'r') except IOError as ioex: print 'errno:', ioex.errno print 'err code:', errno.errorcode[ioex.errno] print 'err message:', os.strerror(ioex.errno) http://docs.python.org/library/errno.html http://docs.python.org/library/os.html For more information on IOError attributes, see the base class EnvironmentError: http://docs.python.org/library/exceptions.html?highlight=ioerror#exceptions.EnvironmentError share|improve this answer edited Jun 28 at 20:15 answered Jul 15 '09 at 23:19 ars 58.1k1696109 1 bett
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this http://stackoverflow.com/questions/5627425/what-is-a-good-way-to-handle-exceptions-when-trying-to-read-a-file-in-python site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up What is python exception a good way to handle exceptions when trying to read a file in python? up vote 9 down vote favorite 2 I want to read a .csv file in python. I don't know if the file exists. My current solution is below. It feels sloppy to me because the two separate exception tests are awkwardly juxtaposed. Is there prettier way to do it? import csv python error handling fName = "aFile.csv" try: with open(fName, 'rb') as f: reader = csv.reader(f) for row in reader: pass #do stuff here except IOError: print "Could not read file:", fName python file-io exception-handling share|improve this question edited Apr 11 '11 at 21:08 asked Apr 11 '11 at 20:51 CharlesHolbrow 1,05431621 add a comment| 4 Answers 4 active oldest votes up vote 6 down vote accepted I guess I misunderstood what was being asked. Re-re-reading, it looks like Tim's answer is what you want. Let me just add this, however: if you want to catch an exception from open, then open has to be wrapped in a try. If the call to open is in the header of a with, then the with has to be in a try to catch the exception. There's no way around that. So the answer is either: "Tim's way" or "No, you're doing it correctly.". Previous unhelpful answer to which all the comments refer: import os if os.path.exists(fName): with open(fName, 'rb') as f: try: # do stuff except : # whatever reader errors you care about # handle error share|improve this answer edited Apr 11 '11