获取错误的错误号
我需要从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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢您澄清您的问题。
Python 中的大多数
异常
都没有“错误号”。一种异常(没有双关语)是HTTPError
异常,例如:另一种异常(如 bobince 所指出的)是
EnvironmentError
:输出
Thanks for clarifying your question.
Most
Exception
s in Python don't have "error numbers". One exception (no pun intended) areHTTPError
exceptions, for example:Another exception (as noted by bobince) are
EnvironmentError
s:outputs
如果您谈论的是
errno.h
错误号,则可以从异常对象的errno
属性中获取它们,但仅限于EnvironmentError
(其中包括OSError
、IOError
和WindowsError
)。特别是在
WindowsError
上,您还将获得一个winerror
属性,其中包含 Windows 特定的错误号。 (不过,您并不经常看到其中之一,因为 Python 直接使用 Win32 API 的情况相对较少。)If you're talking about
errno.h
error numbers, you can get them from theerrno
property on the exception object, but only onEnvironmentError
(which includesOSError
,IOError
andWindowsError
).Specifically on
WindowsError
you'll also get awinerror
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.)还有
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?