What exception shall I raise when os.path.exists fails?

Chris Rebert clp2 at rebertia.com
Thu Oct 29 06:04:52 CET 2009

On Wed, Oct 28, 2009 at 10:01 PM, Peng Yu <pengyu.ut at gmail.com> wrote:
> I want to raise an exception when os.path.exists fails. I looked
> through the exception list on the menu. I'm not sure if IOError is the
> most appropriate exception I should use. Could somebody let me know?

Yeah, I'd say IOError:

Python 2.6.2 (r262:71600, May 14 2009, 16:34:51)
[GCC 4.0.1 (Apple Inc. build 5484)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> f=file('nonexistent', 'r')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'nonexistent'

ValueError would also be a justifiable choice; the caller gave a value
for a filepath that was invalid in that it was non-existent.


More information about the Python-list mailing list