【问题标题】:KeyboardInterrupt close failed in file object destructor: sys.excepthook is missing lost sys.stderr文件对象析构函数中的键盘中断关闭失败:sys.excepthook 丢失了丢失的 sys.stderr
【发布时间】:2013-12-02 04:34:27
【问题描述】:
#!/usr/bin/env python 


import sys, re 




def find_position(line):
    pun = ""
    if re.search(r"[.?!]+", line):
        pun = re.search(r"[.?!]+", line).group()
    pos = line.find(pun)
    pos = pos+len(pun)-1
    return pos




def sentence_splitter(filename):

    f = open(filename, "r")

    for line in f:
        line = line.strip()
        print line + "\n"
        while line:
            pos  =  find_position(line)
            line2 = line[ : pos+1].split(" ")
            length = len(line2)
            last_word = line2[length -1]

        try:
                if re.search(r"[A-Z]+.*", last_word) or  line[pos+1] != " " or line[pos+2].islower() :
                print line[:pos+1],
                line = line[pos+1:]

            else:
            print line[ : pos+1]
                line = line[pos+1 :]


            except :
                print " error here!!"      

    f.close()        
    return " bye bye"





if __name__=="__main__":
    print sentence_splitter(sys.argv[1])

在执行时

python sentence_splitter6.py  README  | more

发生错误

KeyboardInterrupt
close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr

我也必须按 clr+c

它不是自己关闭的

对此进行了尝试 How to handle a file destructor throwing an exception?

How to silence "sys.excepthook is missing" error?

链接也有,但不满意请帮忙

【问题讨论】:

  • 检查sentence_splitter函数中的缩进。在 python 中需要正确的缩进。

标签: python


【解决方案1】:

首先,你的问题就在这里:

while line:
    pos = find_position(line)
    line2 = line[:pos + 1].split(" ")
    length = len(line2)
    last_word = line2[length - 1]

line没有被修改,所以如果它一次为真,它永远为真,while没有办法结束。

那么,KeyboardInterrupt 不是来自你的执行,而是来自你按下C-c,停止你的程序。

您在编写 python 代码时也应该尊重 PEP8,您也可以使用flakes8 和/或pylint 进行检查。

这是符合 PEP8 的版本(仍然有无限循环):

#!/usr/bin/env python3

import sys, re


def find_position(line):
    pun = ""
    if re.search(r"[.?!]+", line):
        pun = re.search(r"[.?!]+", line).group()
    pos = line.find(pun)
    pos = pos+len(pun)-1
    return pos


def sentence_splitter(filename):
    with open(filename, "r") as infile:
        for line in infile:
            line = line.strip()
            print(line + "\n")
            while line:
                pos = find_position(line)
                line2 = line[:pos + 1].split(" ")
                length = len(line2)
                last_word = line2[length - 1]
            if ((re.search(r"[A-Z]+.*", last_word) or
                 line[pos+1] != " " or
                 line[pos+2].islower())):
                print(line[:pos+1], end='')
                line = line[pos+1:]
            else:
                print(line[:pos + 1])
                line = line[pos + 1:]
        return " bye bye"

if __name__ == "__main__":
    print(sentence_splitter(sys.argv[1]))

最后,您应该注释您的代码,以便包括您在内的每个人都能理解您在做什么,例如:

def find_position(line):
    """Finds the position of a pun in the line.
    """

另外find_pun_position 可能是一个更好的名字...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    相关资源
    最近更新 更多