Pythonic way for handling file errors

W

wink

Hello,

I would like to know what would be considered the most
Pythonic way of handling errors when dealing with files,
solutions that seem reasonable using 2.5:

-------
try:
f = open('afile', 'r')
content = f.read()
error = 200
except Exception:
error = 404
finally:
if locals().has_key('f'):
f.close()
------
try:
f = open('afile', 'r')
content = f.read()
except Exception:
error = 404
else:
error = 200
finally:
if locals().has_key('f'):
f.close()
-------
try:
f = open('afile', 'r')
content = f.read()
error = 200
except Exception:
error = 404
finally:
try:
f.close()
except Exception:
pass
-------
try:
f = None
f = open('afile', 'r')
content = f.read()
error = 200
except Exception:
error = 404
finally:
if f:
f.close()
----
try:
with open('afile', 'r') as f:
content = f.read()
error = 200
except Exception:
error = 404
----

Of the above I think I like the last one best, but I think
I'd really like to have:

with open('afile', 'r') as f with exceptions:
content = f.read()
error = 200
except Exception:
error = 404

Another words from looking at PEP343 it is the author
of the object returned by the with expression that gets
to decide if exceptions are re-raised. But it would seem
to me it should be the programmer using it that should
decide.

Of course as a newbie, I may be way off base.

Thanks,

Wink Saville
 
P

Paul Hankin

I would like to know what would be considered the most
Pythonic way of handling errors when dealing with files,
solutions that seem reasonable using 2.5:

The best way to handle errors is to catch the exceptions that are
raised by the code that handles the error. Your examples push the
problem elsewhere: you're setting an error code which has to be tested
for. But perhaps your application needs to do this for some reason
(judging from your error codes, this is some sort of web script).
...
try:
with open('afile', 'r') as f:
content = f.read()
error = 200
except Exception:
error = 404

Of all your examples, this is the best. But the catch-all exception
handler is bad: it's better to catch just file io exceptions. Also, I
think it's better to put the OK case (error 200) in an else clause to
make it clearer that it's only set when no error occurs. It's also
better to use constants in place of magic numbers.

import httplib

try:
with open('afile', 'r') as f:
content = f.read()
except IOError:
error = httplib.NOT_FOUND
else:
error = httplib.OK
 
D

danielx

The best way to handle errors is to catch the exceptions that are
raised by the code that handles the error. Your examples push the
problem elsewhere: you're setting an error code which has to be tested
for. But perhaps your application needs to do this for some reason
(judging from your error codes, this is some sort of web script).


Of all your examples, this is the best. But the catch-all exception
handler is bad: it's better to catch just file io exceptions. Also, I
think it's better to put the OK case (error 200) in an else clause to
make it clearer that it's only set when no error occurs. It's also
better to use constants in place of magic numbers.

import httplib

try:
with open('afile', 'r') as f:
content = f.read()
except IOError:
error = httplib.NOT_FOUND
else:
error = httplib.OK

Wink,

One of the problems your facing is knowing whether you managed to open
the file before reaching the finally block where you close your file.
To avoid this, I open my files right before try/finally. For me, the
only purpose in having the try/finally is to make sure my files are
properly closed, although when I'm lazy, I just let the file close it
self during garbage collection. This is what it looks like:

file = open('filename')
try:
# read and/or process file
finally:
file.close()

Of course, this example does not handle any exceptions. In many cases,
you want these errors to propogate upward so the users of your
functions/methods can decide what needs to be done. On the other hand,
you could wrap this code with try/except/else if you wanted to handle
the exception "at the source".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top