在Python中获取异常详细信息 [英] Getting exception details in Python

查看:182
本文介绍了在Python中获取异常详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须打开&在同一个循环中写入大约10个不同的文件。
例如:

  for i in range(0,10):
try:
a = 5
file1 = open(file1.txt,'w +')
file2 = open(file2.txt,'w +')
#... etc

print(str(a),file = file1)
print(str(a)+file = file2)
#... etc
除了:
#error处理

现在我想做的是能够具体异常信息,例如在一般异常中什么文件被打开/写入。从目前的理解来看,我必须做这样的事情来达到我想要的目的:

  for i in range(0 ,5):
a = 5
try:
file1 = open(file1.txt,'w +')
print(str(a),file = file1)
除了
#error处理file1
try:
file2 = open(file2.txt,'w +')
print(str(a)+file = file2)
除了:
#error处理file2

...当我需要为大约10个不同的文件做这个时,哪个将变得非常笨重和没有吸引力。有没有办法得到(例如)文件名信息的一般的异常,像我的第一个例子?基本上这样的例外可以报告诸如在写入file1时出现错误,而不需要尝试/除了特别为file1操作。



编辑:这是一个大规模的过度简化数据被写入文件。 str(a)和str(a)+hi并不是实际写入数据的真正好的表示; file1可能需要一个硬编码的整数,其中file2可能需要使用多个变量格式化的字符串。将开/写过程推广到循环不会很好地工作。

解决方案

您可以使用 sys.exc_info 获取有关正在处理的异常的信息,包括异常对象本身。一个 IOError 异常包含您需要的所有信息,包括文件名,errno和描述错误的字符串:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ $ b除了IOError:
类型,值,traceback = sys.exc_info()
print('错误打开%s:%s'%(value.filename,value.strerror))

try 块中的执行显然会在第一个例外。


I have to open & write to about 10 different files all within the same loop. e.g:

for i in range(0,10):
    try:
        a=5
        file1 = open("file1.txt",'w+')
        file2 = open("file2.txt",'w+')
        #... etc

        print(str(a),file=file1)
        print(str(a)+"hi",file=file2)
        # ... etc
    except: 
        #error handling

Now what I'd like to do is be able to get specific exception information such as what file was being opened/written to within the general exception. From my current understanding, I'd have to do something like this to achieve what I want:

for i in range(0,5):
    a=5
    try:
        file1 = open("file1.txt",'w+')
        print(str(a),file=file1)
    except: 
        #error handling for file1
    try:
        file2 = open("file2.txt",'w+')
        print(str(a)+"hi",file=file2)
    except: 
        #error handling for file2

...Which is going to get extremely clunky and unattractive when I have to do this for about 10 different files. Is there any way to get (for example) the filename info out of a general exception like in my first example? Basically so the exception could report things like "error when writing to file1" without a try/except specifically for file1 operations.

edit: This is a massive over-simplification of the data being written to the file. str(a) and str(a)+"hi" are not really good representations of the data actually being written; file1 may need a hardcoded integer, where file2 may need a string formatted with multiple variables. to generalize the opening/writing process into a loop isn't going to work very nicely.

解决方案

You can use sys.exc_info to get information about the exception currently being handled, including the exception object itself. An IOError exception contains all of the information you need, including the filename, the errno, and a string describing the error:

import sys

try:
    f1 = open('example1')
    f2 = open('example2')
except IOError:
    type, value, traceback = sys.exc_info()
    print('Error opening %s: %s' % (value.filename, value.strerror))

Execution in the try block will obviously still halt after the first exception.

这篇关于在Python中获取异常详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆