python - Python-将任何/所有潜在的错误写入文件,而没有定义可能出错的部分?

标签 python python-2.7 exception-handling error-handling syntax-error

我正在寻找确保所有错误都保存到文本文件的帮助,但是似乎无法找到一种方法来确保所有错误都在不定义异常处理应关注的部分的情况下保存到文件中。

我浏览了http://docs.python.org/2/tutorial/errors.html,但找不到适合我的需求的东西:/

我敢肯定必须有一个简单的方法来做到这一点。
提前致谢
-Hyflex

最佳答案

import sys
sys.stderr = open("some_log.txt","wb")

我认为至少...
>>> import StringIO
>>> import sys
>>> s = StringIO.StringIO()
>>> sys.stderr = s
>>> 5=7
>>> 5/0
>>> s.seek(0)
>>> s.read()
'  File "<stdin>", line 1\nSyntaxError: can\'t assign to literal\nTraceback (mos
t recent call last):\n  File "<stdin>", line 1, in <module>\nZeroDivisionError:
integer division or modulo by zero\n'

关于python - Python-将任何/所有潜在的错误写入文件,而没有定义可能出错的部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17245719/

相关文章:

python - xlrd、xlwt、xlutils 如何在低级别与 Excel 配合使用?

python - Matplotlib - 标记每个 bin

Python 2.7 Anaconda Pandas 错误(Ubuntu 14.04)

haskell - Haskell 异常处理的正确设计

python - 用 Python 捕获 ConnectionResetError

python - TeamCity 不接受数组作为参数值

python - 从 dict 制作 Dataframe

Python 循环导入, `from lib import module` 与 `import lib.module`

python - 如何编写一个 python 脚本来不断尝试网页直到它打开

delphi - 如何替换除 block 之外的空?