100

I use a third-party library that's fine but does not handle inexistant files the way I would like. When giving it a non-existant file, instead of raising the good old

FileNotFoundError: [Errno 2] No such file or directory: 'nothing.txt'

it raises some obscure message:

OSError: Syntax error in file None (line 1)

I don't want to handle the missing file, don't want to catch nor handle the exception, don't want to raise a custom exception, neither want I to open the file, nor to create it if it does not exist.

I only want to check it exists (os.path.isfile(filename) will do the trick) and if not, then just raise a proper FileNotFoundError.

I tried this:

#!/usr/bin/env python3

import os

if not os.path.isfile("nothing.txt"):
    raise FileNotFoundError

what only outputs:

Traceback (most recent call last):
  File "./test_script.py", line 6, in <module>
    raise FileNotFoundError
FileNotFoundError

This is better than a "Syntax error in file None", but how is it possible to raise the "real" python exception with the proper message, without having to reimplement it?

zezollo
  • 4,606
  • 5
  • 28
  • 59

2 Answers2

187

Pass in arguments:

import errno
import os

raise FileNotFoundError(
    errno.ENOENT, os.strerror(errno.ENOENT), filename)

FileNotFoundError is a subclass of OSError, which takes several arguments. The first is an error code from the errno module (file not found is always errno.ENOENT), the second the error message (use os.strerror() to obtain this), and pass in the filename as the 3rd.

The final string representation used in a traceback is built from those arguments:

>>> print(FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), 'foobar'))
[Errno 2] No such file or directory: 'foobar'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    Perfect! I was so struggling to find the right way of writing this. – zezollo Mar 18 '16 at 06:30
  • @zezollo: I've updated the answer; I found `os.strerror()` now (I *knew* there was a mapping from error number to string message somewhere). – Martijn Pieters Mar 18 '16 at 06:35
  • 1
    I find it somewhat odd that you are required to pass the error number as an argument when `FileNotFoundError` is documented as corresponding to `ENOENT`. (Similar arguments apply to other subclasses of `OSError`.) Maybe this should be a separate question, but: why is that? – chepner Apr 19 '18 at 14:19
  • 3
    @chepner: the `OSError()` class does that mapping; `OSError(errno.ENOENT, os.strerror(errno.ENOENT), filename)` returns a `FileNotFound()` instance. But to create such an instance, you still have to pass in those same arguments. If you have the `errno` constant in a variable `e`, stick to `OSError(e, os.strerror(e), path)`. The subclasses all provide the same interface as the base class. – Martijn Pieters Apr 20 '18 at 00:20
  • So it seems you need between 2 and 5 arguments to make OSError assign them to properties. I already found out that the fith argument is mapped to `filename2` but was unable to find out about the fourth one. Does anybody know? – Bachsau Dec 03 '18 at 17:51
  • 4
    In Python 2 `FileNotFoundError` is not defined, so you should either raise `IOError`/`OSError`, or define it as a subclass of those (see [this question](https://stackoverflow.com/q/26745283/4614641)). – PlasmaBinturong Jan 24 '19 at 16:46
  • 1
    @PlasmaBinturong: no, which is why this question is tagged with [tag:python-3.x] :-) – Martijn Pieters Jan 24 '19 at 16:50
  • 1
    Reading [the doc of `FileNotFoundError`](https://docs.python.org/3/library/exceptions.html#FileNotFoundError) ("FileNotFoundError [...] Corresponds to errno ENOENT") guides me to the point: Isn't the 1st and 2nd parameter of FileNotFoundError construction redundant to the class name? (Anyway the answer seams to be exact since there is a diff of `print(FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), 'foobar'))` and `print(FileNotFoundError('foobar')`.) – 550 Jun 09 '22 at 07:06
  • @550: it's because the class inherits the base `OSError` constructor; if you use `OSError(errno.ENOENT, os.strerror(errno.ENOENT), filename)` you get a `FileNotFoundError` instance back. The subclasses are there _mainly_ to make `try..except` code easier and clearer. – Martijn Pieters Jul 07 '22 at 13:56
  • @550 Reading through the [original bug log](https://bugs.python.org/issue12555) I'm surmising that this makes it easier to create custom subclasses; all subclasses accept the same `__init__` arguments this way. – Martijn Pieters Jul 07 '22 at 14:14
5

In Python, a variable can refer to the type (class), or an object (instance of the class):

>>> x = FileNotFoundError
>>> print(type(x))
<class 'type'>

>>> x = FileNotFoundError()
>>> print(type(x))
<class 'FileNotFoundError'>

While it's possible to also throw the type FileNotFoundError, you practically always want to throw an object that has been constructed from the class. The constructor accepts the same arguments as OSError. You can pass a standard POSIX and Windows error code, but it's enough to pass an error message. (In your case the standard error message "No such file or directory" is not entirely accurate, since you also throw the error if a directory is found.)

if not os.path.isfile("nothing.txt"):
    raise FileNotFoundError("nothing.txt was not found or is a directory")
Seppo Enarvi
  • 3,219
  • 3
  • 32
  • 25