Python-future: Any support for FileNotFoundError?

Created on 3 Dec 2016  ·  5Comments  ·  Source: PythonCharmers/python-future

In Python 3.3+ we can handle missing files easily:

try:
    open('unobtanium')
except FileNotFoundError:
    ...

In Python 2 there is a bit more song and dance:

try:
    open('unobtanium')
except IOError as err:
    if err.errno != errno.ENOENT:
        raise

Does this library allow us to write the simpler Python 3 style file handling in Python 2, somehow? If not, are there any plans to get FileNotFoundError into python-future, or is it too difficult to implement?

Thanks!

Most helpful comment

@edschofield Are these going to make it into a release any time soon? Shall I close this issue? Thanks!

All 5 comments

Thanks for the request, Wim. This sounds like a good feature to have.

Are you able to contribute a patch that defines FileNotFoundError in src/future/builtins/__init__.py? This would speed up the process of getting it supported. :-)

I have committed a patch to a new development branch called exceptions. You can use the new PEP3151 exceptions with code like this:

from builtins import FileNotFoundError

at the top of modules that invoke those exceptions.

Could you please try the new branch and report back about how well this works for you?

Hi Ed, yes, I tried it and it seems to work fine:

>>> import sys; sys.version
'2.7.12 (default, Jun 29 2016, 14:05:02) \n[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)]'
>>> from builtins import FileNotFoundError
>>> try:
...     open('unobtanium')
... except FileNotFoundError as err:
...     pass
... 
>>> err
IOError(2, 'No such file or directory')
>>> issubclass(FileNotFoundError, OSError)
True

@edschofield Are these going to make it into a release any time soon? Shall I close this issue? Thanks!

I believe this is included in 0.17

Was this page helpful?
0 / 5 - 0 ratings

Related issues

asottile picture asottile  ·  18Comments

ankostis picture ankostis  ·  4Comments

e-rk picture e-rk  ·  14Comments

foreignmeloman picture foreignmeloman  ·  3Comments

wagnerpeer picture wagnerpeer  ·  12Comments