如何从Python 3导入FileNotFoundError ?

时间:2022-03-28 20:24:11

I am currently using Python 2 on a project that needs a Python 3 built-in exception: FileNotFoundError. How do I do it?

我目前正在一个需要Python 3内置异常的项目上使用Python 2: FileNotFoundError。我该怎么做呢?

3 个解决方案

#1


22  

You can of course define any exceptions you want.

当然,您可以定义任何您想要的异常。

But they're not going to do you any good. The whole point of FileNotFoundError is that any Python operation that runs into a file-not-found error will raise that exception. Just defining your own exception won't make that true. All you're going to get is an OSError (or IOError, depending on 2.x version) with an appropriate errno value. If you try to handle a custom FileNotFoundError, your handler will never get called.

但它们不会对你有任何好处。FileNotFoundError的全部意义在于,任何遇到文件找不到的错误的Python操作都会引发该异常。仅仅定义你自己的异常是不能证明这一点的。你只会得到一个OSError(或者IOError,取决于2)。x版本)具有适当的errno值。如果您试图处理自定义的FileNotFoundError,您的处理程序将永远不会被调用。

So, what you really want is (for example):

所以,你真正想要的是(例如):

try:
    f = open(path)
except OSError as e:
    if e.errno == errno.ENOENT:
        # do your FileNotFoundError code here
    else:
        raise

#2


2  

You can simply create the FileNotFoundError exception yourself:

您可以自己创建FileNotFoundError异常:

class FileNotFoundError(OSError):
    pass

This new exception class inherits from OSError just like the one in Python 3.x.

这个新的异常类继承了OSError,就像Python 3.x中的那个一样。

Here is a reference on User-defined Exceptions in Python.

下面是对Python中的用户定义异常的引用。

#3


2  

You could use IOError instead:

你可以用IOError代替:

Raised when an I/O operation (such as a print statement, the built-in open() function or a method of a file object) fails for an I/O-related reason, e.g., “file not found” or “disk full”.

当I/O操作(如打印语句、内置的open()函数或文件对象的方法)由于I/O相关的原因失败时,例如“文件未找到”或“磁盘已满”,就会引发此问题。

This class is derived from EnvironmentError. See the discussion above for more information on exception instance attributes.

这个类来自于环境。有关异常实例属性的更多信息,请参见上面的讨论。

Changed in version 2.6: Changed socket.error to use this as a base class.

在版本2.6中更改:已更改套接字。使用它作为基类是错误的。

#1


22  

You can of course define any exceptions you want.

当然,您可以定义任何您想要的异常。

But they're not going to do you any good. The whole point of FileNotFoundError is that any Python operation that runs into a file-not-found error will raise that exception. Just defining your own exception won't make that true. All you're going to get is an OSError (or IOError, depending on 2.x version) with an appropriate errno value. If you try to handle a custom FileNotFoundError, your handler will never get called.

但它们不会对你有任何好处。FileNotFoundError的全部意义在于,任何遇到文件找不到的错误的Python操作都会引发该异常。仅仅定义你自己的异常是不能证明这一点的。你只会得到一个OSError(或者IOError,取决于2)。x版本)具有适当的errno值。如果您试图处理自定义的FileNotFoundError,您的处理程序将永远不会被调用。

So, what you really want is (for example):

所以,你真正想要的是(例如):

try:
    f = open(path)
except OSError as e:
    if e.errno == errno.ENOENT:
        # do your FileNotFoundError code here
    else:
        raise

#2


2  

You can simply create the FileNotFoundError exception yourself:

您可以自己创建FileNotFoundError异常:

class FileNotFoundError(OSError):
    pass

This new exception class inherits from OSError just like the one in Python 3.x.

这个新的异常类继承了OSError,就像Python 3.x中的那个一样。

Here is a reference on User-defined Exceptions in Python.

下面是对Python中的用户定义异常的引用。

#3


2  

You could use IOError instead:

你可以用IOError代替:

Raised when an I/O operation (such as a print statement, the built-in open() function or a method of a file object) fails for an I/O-related reason, e.g., “file not found” or “disk full”.

当I/O操作(如打印语句、内置的open()函数或文件对象的方法)由于I/O相关的原因失败时,例如“文件未找到”或“磁盘已满”,就会引发此问题。

This class is derived from EnvironmentError. See the discussion above for more information on exception instance attributes.

这个类来自于环境。有关异常实例属性的更多信息,请参见上面的讨论。

Changed in version 2.6: Changed socket.error to use this as a base class.

在版本2.6中更改:已更改套接字。使用它作为基类是错误的。