我的程序使用python写入文件。除非我已经运行了该文件并保持文件打开,否则一切都进行得很顺利,并抛出一个IOError: Errno 13权限被拒绝:“路径名”。
我想知道退出程序并启动消息框向用户解释问题的最佳方法,并指示关闭先前的文件以进行覆盖。
def myFunction(self):
with open(self.pathString, 'wb') as ofile:
writer = csv.writer(ofile, quoting=csv.QUOTE_NONE, delimiter='\t')
...
if IOError:
QMessageBox.information(None, 'Information', 'A message')
return发布于 2014-09-30 19:20:58
这很容易:
try:
result = do_here_what_can_cause_IOError
except IOError as e:
log.error('Error when trying to perform do_here_what_can_cause_IOError: %s', e)
else:
use_the_result_here总之,要捕获Python抛出的任何类型的异常,请使用try/else/ use /finally
文档这里
https://stackoverflow.com/questions/26128358
复制相似问题