OSError的文件名属性不可用?
我有以下代码:
except(OSError) as (errno, strerror, filename):
print "OSError [%d]: %s at %s" % (errno, strerror, filename)
除非满足 OSError num,否则它运行良好。 123(文件名、目录名或卷标语法不正确
)。然后我在 except 代码行收到以下错误:
ValueError: need more than 2 values to unpack
这是通过不使用 filename
属性解决的。然而我的要求阻止我不使用这个属性。
还有别的办法吗?
I have the following code:
except(OSError) as (errno, strerror, filename):
print "OSError [%d]: %s at %s" % (errno, strerror, filename)
It runs great unless it meets OSError num. 123 (The file name, directory name, or volume label syntax is incorrect
). then I get the following error at the except code line:
ValueError: need more than 2 values to unpack
It is solved by not using the filename
attribute. However my requirements prevent me from not using this attribute.
Is there another way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有见过这种将 Exception 对象的属性传递给 as 子句的异常处理。
通常,您将 ExceptionObject 除外作为 e 处理,并像通常处理对象的属性一样处理属性。
OSError 包含 errno 属性是 errno 中的数字错误代码,strerror 属性是相应的字符串,对于涉及文件系统路径的异常(例如 chdir() 或 unlink()),异常实例将包含第三个属性, filename,这是传递给函数的文件名。
I have not seen this kind of Exception handling where you are passing the Exception object's attributes to the as clause.
Normally you handle
except ExceptionObject as e
and handle the attributes as one would normally handle the attributes of an object.OSError contains a errno attribute is a numeric error code from errno, and the strerror attribute is the corresponding string and for exceptions that involve a file system path (such as chdir() or unlink()), the exception instance will contain a third attribute, filename, which is the file name passed to the function.