I have several possible files which could hold my data; they can be compressed in different ways, so to open them I need to use file()
, gzip.GzipFile()
and other which also return a file object (supporting the with
interface).
I want to try each of them until one succeeds in opening, so I could do something like
try:
with gzip.GzipFile(fn + '.gz') as f:
result = process(f)
except (IOError, MaybeSomeGzipExceptions):
try:
with xCompressLib.xCompressFile(fn + '.x') as f:
result = process(f)
except (IOError, MaybeSomeXCompressExceptions):
try:
with file(fn) as f:
result = process(f)
except IOError:
result = "some default value"
which obviously isn't feasible in case I have dozens of possible compression variants. (The nesting will get deeper and deeper, the code always looking very much alike.)
Is there a nicer way to spell this out?
EDIT: If possible I'd like to have the process(f)
out of the try/except as well to avoid accidental catching of exceptions raised in the process(f)
.