Cross-platform detection of exceptions raised during file access via os, shutil, codecs, etc.

python at bdurham.com python at bdurham.com
Wed Jun 9 17:56:59 EDT 2010


I'm looking for some suggestions on how to detect exceptions
raised during common types of file access (os, shutil, codecs,
etc.) on a cross-platform basis. I'm looking for feedback
relative to Python 2.6 and 2.7 but would also appreciate hearing
of any Python 3.x specific behaviors.

Under Python 2.6 for Windows, we're trapping file activity
exceptions via the following:

except ( WindowsError, IOError ):

According to the Exception Hierarchy on this page[1] (and snipped
below), I suspect that most/all(?) of my file access exceptions
"should" derive from EnvironmentErrors. In order to make our code
more portable, should we trap EnvironmentError, IOError, OSError,
AND WindowsErrors or is that too broad?

   |    +-- EnvironmentError
   |    |    +-- IOError
   |    |    +-- OSError
   |    |         +-- WindowsError (Windows)
   |    |         +-- VMSError (VMS)


Part of my confusion/discomfort is the fact that Windows errors
are not mapped back to the OSError exception, but instead exist
in their own subclass. I'm curious what makes Windows file access
so special that it needs its own custom exception class?
Thank you,
Malcolm

[1] http://docs.python.org/library/exceptions.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100609/182eb5b1/attachment.html>


More information about the Python-list mailing list