Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any support for FileNotFoundError? #254

Closed
wimglenn opened this issue Dec 3, 2016 · 5 comments
Closed

Any support for FileNotFoundError? #254

wimglenn opened this issue Dec 3, 2016 · 5 comments

Comments

@wimglenn
Copy link

wimglenn commented Dec 3, 2016

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!

@edschofield
Copy link
Contributor

edschofield commented Dec 4, 2016 via email

@edschofield
Copy link
Contributor

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?

@wimglenn
Copy link
Author

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

@wimglenn
Copy link
Author

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

olala7846 referenced this issue in foundi/pypugjs Jan 19, 2018
Use IOError instead of FileNotFoundError for python2 support
@jmadler
Copy link
Contributor

jmadler commented May 6, 2019

I believe this is included in 0.17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants