获取错误的错误号

发布于 2024-09-10 05:52:56 字数 315 浏览 4 评论 0原文

我需要从Python中发生的错误中获取错误号。

前任;当尝试通过 Paramiko 包传输目录时,这段代码捕获了一个错误:

try:
    sftp.put(local_path,target_path)    
except (IOError,OSError),errno:
    print "Error:",errno

对于我获得的输出,

Error: [Errno 21] Is a directory

我想利用错误号进入更多代码来传输目录和目录内容。

I need to obtain the error number from an error that has occurred in Python.

Ex; When trying to transfer a directory via the Paramiko package, an error is caught with this piece of code:

try:
    sftp.put(local_path,target_path)    
except (IOError,OSError),errno:
    print "Error:",errno

For which I get the output,

Error: [Errno 21] Is a directory

I want to utilize the error number to go into some more code to transfer the directory and the directory contents.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如梦 2024-09-17 05:52:56

感谢您澄清您的问题。

Python 中的大多数异常都没有“错误号”。一种异常(没有双关语)是 HTTPError 异常,例如:

import urllib2 
try:
   page = urllib2.urlopen("some url")
except urllib2.HTTPError, err:
   if err.code == 404:
       print "Page not found!"
   else:
       ...

另一种异常(如 bobince 所指出的)是 EnvironmentError

import os
try:
   f=open("hello")
except IOError, err:
   print err
   print err.errno
   print err.strerror
   print err.filename

输出

[Errno 2] No such file or directory: 'hello'
2
No such file or directory
hello

Thanks for clarifying your question.

Most Exceptions in Python don't have "error numbers". One exception (no pun intended) are HTTPError exceptions, for example:

import urllib2 
try:
   page = urllib2.urlopen("some url")
except urllib2.HTTPError, err:
   if err.code == 404:
       print "Page not found!"
   else:
       ...

Another exception (as noted by bobince) are EnvironmentErrors:

import os
try:
   f=open("hello")
except IOError, err:
   print err
   print err.errno
   print err.strerror
   print err.filename

outputs

[Errno 2] No such file or directory: 'hello'
2
No such file or directory
hello
仅此而已 2024-09-17 05:52:56

如果您谈论的是 errno.h 错误号,则可以从异常对象的 errno 属性中获取它们,但仅限于 EnvironmentError (其中包括 OSErrorIOErrorWindowsError)。

特别是在 WindowsError 上,您还将获得一个 winerror 属性,其中包含 Windows 特定的错误号。 (不过,您并不经常看到其中之一,因为 Python 直接使用 Win32 API 的情况相对较少。)

If you're talking about errno.h error numbers, you can get them from the errno property on the exception object, but only on EnvironmentError (which includes OSError, IOError and WindowsError).

Specifically on WindowsError you'll also get a winerror property with a Windows-specific error number. (You don't often see one of these though, as Python uses the Win32 API directly relatively rarely.)

回首观望 2024-09-17 05:52:56

还有 errno包,它允许处理错误代码而不必处理代码中的幻数。请参阅此处的示例:与 IOError 关联的 Python 错误号稳定吗?

There is also the errno package, which allows working with error codes with out having to handle magic numbers in the code. See an example here: Are Python error numbers associated with IOError stable?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文