question about try/except blocks

J

J

I have this function in a class:

def write_file(self, data, dest):
with open(dest, 'wb', 0) as outfile:
try:
print("IN WRITE_FILE")
outfile.write(self.data)
except IOError as exc:
logging.error("Unable to write data to %s: %s", dest, exc)
return False
else:
outfile.flush()
os.fsync(outfile.fileno())
return True


Which is simply called like this:
test.write_file(test.data, target_file)

However, when this is called on a read-only filesystem, it throws an OSError:

File "./scripts/removable_storage_test", line 118, in write_file
with open(dest, 'wb', 0) as outfile:
OSError: [Errno 30] Read-only file system:
'/media/bladernr/5747-AD2E/tmpfbzsxk.0'

So what I am not sure of is how to handle this?

Would it be better to wrap the call and catch the OSError there, or
wrap the whole with open() block in the function itself?

My thought is to wrap the with open() call in the function so that I'm
not wrapping the function call every time I use the class somewhere,
but then I am not sure of that as it leads to nested try blocks like
so:

try:
with open(dest, 'wb', 0) as outfile:
try:
stuff
except IOError as exec:
more stuff
else:
other stuff
except OSError as exc:
error handling stuff
return False


I think, functionally, that should work, but should nested try/except
blocks be avoided?
 
S

Steven D'Aprano

I have this function in a class:

def write_file(self, data, dest):
with open(dest, 'wb', 0) as outfile:
try:
print("IN WRITE_FILE")
outfile.write(self.data)
except IOError as exc:
logging.error("Unable to write data to %s: %s", dest,
exc) return False
else:
outfile.flush()
os.fsync(outfile.fileno())
return True [...]
I think, functionally, that should work, but should nested try/except
blocks be avoided?


Not particularly. Try/except is cheap to set up, nesting them doesn't
cost much.

But having said that, your code as given does not protect against rare
but possible errors. For example, just because you can open the file for
writing doesn't mean you can write to it; just because you can write to
it doesn't mean flush will succeed; just because flush succeeds doesn't
mean that syncing to disk will succeed. Any IO operation might fail.
While you can wrap each one individually, it's probably better to wrap
the whole lot at once. I'd write it like this:


def write_file(self, data, dest):
try:
with open(dest, 'wb', 0) as outfile:
print("IN WRITE_FILE")
outfile.write(self.data)
outfile.flush()
os.fsync(outfile.fileno())
except (OSError, IOError) as exc:
logging.error("Error writing data to %s: %s", dest, exc)
return False
return True



File IO is one of the rare exceptions to the idea that try/except blocks
should wrap the least amount of code possible.
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top