Ricardo Aráoz wrote:
> Jeff Younker wrote:
>> The enclosing try block isn't needed. More on this later.
> Maybe it is my poor understanding of exception handling. My intention
> here is to open the first file, if error then report in logging and
> finish normally, else open the 2nd file, if error then report in logging
> close the 1st file and finish normally. If no error then process.
The code is correct for what you want to do. It does seem a bit awkward
but it's hard to get much better and preserve the logging.
You could use 'return' instead of 'raise' and eliminate the outer try block.
You could put the duplicate code in a function (with raise):
def my_open(f):
try :
fIncl = open(f)
except Exception, e :
logging.error('Error!!! No pude abrir "%s" : %s',
f,
e.strerror)
raise
but you would still need try/except in the calling function:
try :
fIncl = my_open(mensaje.direcciones)
except:
return
try :
fExcl = my_open(mensaje.excluidas)
except:
fIncl.close()
return
It is safe to omit the close() in this situation - the file will be
closed by the OS when the program terminates - so you could use a single
try block:
try :
fIncl = my_open(mensaje.direcciones)
fExcl = my_open(mensaje.excluidas)
except:
return
That is the only version I can come up with that seems significantly
simpler than what you wrote.
Kent
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor