Searching for equivalent of FileNotFoundError in Python 2
2017-08-19 16:52
471 查看
I created a class named Options. It works fine but not not with Python 2. And I want it to work on both Python 2 and 3. The problem is identified: FileNotFoundError doesn t exist in Python 2. But if I use IOError it doesn t work in Python 3
Changed in version 3.3: EnvironmentError, IOError, WindowsError, VMSError, socket.error, select.error and mmap.error have been merged into OSError.
You can use the base class exception EnvironmentError and use the 'errno' attribute to figure out which exception was raised:
Or just use IOError in the same way:
That works on Python 2 or Python 3.
Be careful not to compare against number values directly, because they can be different on different platforms. Instead, use the named constants in Python's standard library
Changed in version 3.3: EnvironmentError, IOError, WindowsError, VMSError, socket.error, select.error and mmap.error have been merged into OSError.
You can use the base class exception EnvironmentError and use the 'errno' attribute to figure out which exception was raised:
from __future__ import print_function import os import errno try: open('no file of this name') # generate 'file not found error' except EnvironmentError as e: # OSError or IOError... print(os.strerror(e.errno))
Or just use IOError in the same way:
try: open('/Users/test/Documents/test') # will be a permission error except IOError as e: print(os.strerror(e.errno))
That works on Python 2 or Python 3.
Be careful not to compare against number values directly, because they can be different on different platforms. Instead, use the named constants in Python's standard library
errnomodulewhich will use the correct values for the run-time platform.
相关文章推荐
- 'Missing recommended icon file - The bundle does not contain an app icon for iPhone / iPod Touch of exactly '120x120' pixels, in .png format'
- python读取csv文件是报错_csv.Error: iterator should return strings, not bytes (did you open the file in text
- 连接sde服务报错"Entry for SDE instance not found in services file”
- Python使用eval强制转换字符串为字典时报错:File "<string>", line 1, in <module> NameError: name 'nan' is not defined
- MOSS2007- closed problem of ["Cannot make a cache safe URL for "styles/portal.css", file not found.]
- Neutron数据库同步错误 NotImplementedError: No support for ALTER of constraints in SQLite dialect
- Python IOError: File not open for writing
- Archive for required library: "xxxx" in project '*' cannot be read or is not a valid ZIP file
- Error:Could not get unknown property ‘apkVariantData’ for object of type com.android.build.gradle.in
- The bundle does not contain an app icon for iPhone / iPod Touch of exactly '120x120' pixels, in .pn
- python2.7以下出 NameError: global name 'FileNotFoundError' is not defined的解决方案
- 使用cocapods报错 [!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes
- Archive for required library: "xxxx" in project '*' cannot be read or is not a valid ZIP file
- Neutron数据库同步错误 NotImplementedError: No support for ALTER of constraints in SQLite dialect
- entry for sde instance not found in services file解决方法(转)
- filegroup reference and partitioning scheme' is not supported in this version of sql server.
- python csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
- [zz]MD5 Hash of File in Python
- 奇葩问题:This file could not be checked in because the original version of the file on the server was moved or deleted. A new version of this file has been saved to the server, but your check-in comments were not saved
- python解压函数extractall在windows上报错FileNotFoundError [Errno 2] No such file or directory