Searching for equivalent of FileNotFoundError in Python 2
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Searching for equivalent of FileNotFoundError in Python 2相关的知识,希望对你有一定的参考价值。
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:
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 errno
modulewhich will use the correct values for the run-time platform.
以上是关于Searching for equivalent of FileNotFoundError in Python 2的主要内容,如果未能解决你的问题,请参考以下文章
Suggestion in searching for a job in NZ
MobileNetV3:Searching for MobileNetV3
轻量级网络论文精度笔记:《Searching for MobileNetV3》
windows10环境下QtCreator中出现skipping incompatible xxx when searching for xxx 问题解决办法
Oracle-RAC等价性验证错误:Result: PRVF-4007 : User equivalence check failed for user "grid"