0

如果我有以下代码:

try: 
    execfile("script.py")
except ## unsure what exception goes here...
    continue:
try: 
    execfile("other.py")
except ## unsure what exception goes here...
    continue:

如何从 script.py 中捕获所有错误,将其保存到文件中,然后继续执行下一个调用的脚本

任何人有任何想法或线索?

4

2 回答 2

2
errors = open('errors.txt', 'w')
try: 
    execfile("script.py")
except Exception as e:
    errors.write(e)
try: 
    execfile("other.py")
except Exception as e:
     errors.write(e)
errors.close()
于 2013-08-13T00:05:52.613 回答
1
import traceback # This module provides a standard interface to extract, 
                 # format and print stack traces of Python programs.

try: 
    execfile("script.py")
except:
    traceback.print_exc(file=open('script.traceback.txt', 'w')) # Writing exception with traceback to file script.traceback.txt

# Here is the code that will work regardless of the success of running a script.py
于 2013-08-13T00:10:02.327 回答