Re: how do you know if open failed?
- From: "Erik Johnson" <ej at wellkeeper dot com>
- Date: Thu, 28 Sep 2006 12:41:58 -0600
"tobiah" <toby@xxxxxxxxxx> wrote in message
news:451c0669$0$19689$88260bb3@xxxxxxxxxxxxxxxxxxxx
SpreadTooThin wrote:
f = open('myfile.bin', 'rb')try:
How do I know if there was an error opening my file?
open('noexist')
except:
print "Didn't open"
That's a way to trap any exception. I think a better answer to the
question is "You'll know if it didn't work because Python throws exceptions
when it runs into problems." You can catch exceptions and try to do
something about them if you want to. Uncaught exceptions cause the
interpreter to exit with a stack trace. Sometimes that's the most logical
thing to do.
Traceback (most recent call last):fd = open('doesnt_exist', 'rb')
File "<stdin>", line 1, in ?
IOError: [Errno 2] No such file or directory: 'doesnt_exist'
It would throw a different exception if there were a permission problem, for
example.
-ej
.
- References:
- how do you know if open failed?
- From: SpreadTooThin
- Re: how do you know if open failed?
- From: tobiah
- how do you know if open failed?
- Prev by Date: Re: ctypes.c_void_p(-1) might not be C return (void *) -1
- Next by Date: Re: Can string formatting be used to convert an integer to its binary form ?
- Previous by thread: Re: how do you know if open failed?
- Next by thread: Re: how do you know if open failed?
- Index(es):
Relevant Pages
|