Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么';t系统除吊钩工作外?_Python_Sys - Fatal编程技术网

Python 为什么';t系统除吊钩工作外?

Python 为什么';t系统除吊钩工作外?,python,sys,Python,Sys,如果我试图执行此代码,为什么不调用sys.excepthook函数 import sys; def MyExcepthook(ex_cls, ex, tb): print("Oops! There's an Error.\n"); a=open("./ERR.txt","w"); #Fixed as suggested by unutbu BUT the problem is the same! a.write("Oops! There's an Error.\n")

如果我试图执行此代码,为什么不调用
sys.excepthook
函数

import sys;
def MyExcepthook(ex_cls, ex, tb):

    print("Oops! There's an Error.\n");

    a=open("./ERR.txt","w"); #Fixed as suggested by unutbu BUT the problem is the same!
    a.write("Oops! There's an Error.\n");
    a.close();

sys.excepthook = MyExcepthook;

def main():
    print(1/0);

if (__name__=="__main__"):
    main();
输出:

Traceback (most recent call last):
  File "C:\Users\Path\to\my\python\file.py", line 13, in <module>
    main();
  File "C:\Users\Path\to\my\python\file.py", line 10, in main
    print(1/0);
ZeroDivisionError: division by zero
并且应该创建一个新文件(
Err.txt
)(通过
open

由于未调用
sys.excepthook
函数,因此
print
函数不显示文本,并且未创建文件-为什么

-->编辑 我的问题是由空闲Python3.4中的一个bug引起的,因为现在我尝试通过python解释器(命令行)运行代码,它可以工作!如果不警告空闲python 3.4中的这个bug,那么我的问题就没用了。我很抱歉,谢谢您的帮助


[解决方案]如果有人遇到我同样的问题=>请尝试通过命令行运行您的代码!而不是从IDE。

您的自定义excepthook本身不能引发异常:

a=open("./ERR.txt")   # opens the file in read mode
应该是

a=open("./ERR.txt", 'w')  # open the file in write mode.

当自定义excepthook引发异常时,您应该看到 差不多

Oops! There's an Error.

Error in sys.excepthook:
...
IOError: [Errno 2] No such file or directory: './ERR.txt'

Original exception was:
...
ZeroDivisionError: integer division or modulo by zero

别忘了删除所有不必要的分号

import sys
def my_excepthook(ex_cls, ex, tb):
    msg = "Oops! There's an Error.\n"
    print(msg)

    with open("./ERR.txt", 'w') as a:
        a.write(msg)

sys.excepthook = my_excepthook

def main():
    print(1/0)

if __name__=="__main__":
    main()

这看起来确实是个问题。我进一步建议使用
with
语句,而不是手动调用文件上的
close
。我为我的错误道歉!我修复了open()的参数,但问题仍然是一样的!它不起作用@NerdEngine:“不准确”-比如,你发布的代码不能准确反映你实际运行的代码?如果是这样的话,请复制粘贴您运行的实际代码,以便将错误带入您的问题中。不要在问题框中重新键入代码,也不要纠正您注意到的单个不一致之处。否则,我们在这里的时间会比需要的时间长得多,而您解决问题的可能性也会小得多。@user2357112我再次为我的错误道歉,从现在起我将更加警惕。但是现在代码是正确的@MichaelClerx很抱歉你是对的!空闲的Python3.4中有一个bug,因为现在我试图通过python解释器(命令行)运行代码,它可以工作!如果不警告空闲python 3.4中的这个bug,那么我的问题就没用了。我很抱歉,谢谢您的帮助![offtop]您不会在python中使用分号:)
import sys
def my_excepthook(ex_cls, ex, tb):
    msg = "Oops! There's an Error.\n"
    print(msg)

    with open("./ERR.txt", 'w') as a:
        a.write(msg)

sys.excepthook = my_excepthook

def main():
    print(1/0)

if __name__=="__main__":
    main()