Python Open File Error Checking
Contents |
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 about Stack Overflow the company Business Learn more python ioerror about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users syntax for generic except clause in python Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping python print exception message each other. Join them; it only takes a minute: Sign up What is 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 python custom exception .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 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
Is Nested Try Block Possible In Python
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 at 21:23 answered Apr 11 '11 at 20:55 Josh Caswell 52.6k11103152 5 Just because a file exists doesn't mean that you can read it! –Gabe Apr 11 '11 at 20:59 1 This isn't perfect, because it is possible that the file gets deleted (e.g. by another process) between checking that it exists and trying to open it. –Liquid_Fire Apr 11 '11 at 20:59 1 It's also possible that fName could be the name o
you have probably seen some. There are (at least) two distinguishable kinds of errors: syntax errors and exceptions. 8.1. Syntax Errors¶ Syntax
Name Of Errors In Python
errors, also known as parsing errors, are perhaps the most common kind an exception can be in python of complaint you get while you are still learning Python: >>> while True print 'Hello world' File syntax for raise clause in python "
and get tips & solutions from a community of 418,626 IT Pros & Developers. It's https://bytes.com/topic/python/answers/542290-how-do-you-know-if-open-failed quick & easy. how do you know if open failed? P: n/a SpreadTooThin f = open('myfile.bin', 'rb') How do I know if there was an error opening my file? Sep 28 '06 #1 Post Reply Share this Question 5 Replies P: n/a tobiah SpreadTooThin wrote: f = open('myfile.bin', 'rb') How do I know if there was an error in python opening my file? try: open('noexist') except: print "Didn't open" -- Posted via a free Usenet account from http://www.teranews.com Sep 28 '06 #2 P: n/a Fredrik Lundh SpreadTooThin wrote: f = open('myfile.bin', 'rb') How do I know if there was an error opening my file? you'll notice: >>f = open("myfile.bin", "rb") Traceback (most recent call last): File "