我有一个 Python 脚本,其中包含一个读取文件并做一些事情的大循环(我正在使用几个包,如 urllib2、httplib2 或 BeautifulSoup)。
它看起来像这样:
try:
with open(fileName, 'r') as file :
for i, line in enumerate(file):
try:
# a lot of code
# ....
# ....
except urllib2.HTTPError:
print "\n >>> HTTPError"
# a lot of other exceptions
# ....
except (KeyboardInterrupt, SystemExit):
print "Process manually stopped"
raise
except Exception, e:
print(repr(e))
except (KeyboardInterrupt, SystemExit):
print "Process manually stopped"
# some stuff
问题是程序在我点击Ctrl+时停止,C但它没有被我的两个 KeyboardInterrupt 异常中的任何一个捕获,尽管我确信它当前处于循环中(因此至少在大 try/except 内)。
这怎么可能?起初我认为这是因为我正在使用的一个包没有正确处理异常(比如只使用“except:”),但如果是这样的话,我的脚本就不会停止。但是脚本确实停止了,它应该至少被我的两个人抓住,对吧?
我哪里错了?
提前致谢!
编辑:
通过在 try-except 之后添加一个finally:
子句并在两个 try-except 块中打印回溯,它通常None
在我点击Ctrl+时显示C,但我曾经设法得到这个(似乎它来自 urllib2,但我不知道是否这就是我无法捕捉键盘中断的原因):
回溯(最近一次通话最后):
File "/home/darcot/code/Crawler/crawler.py", line 294, in get_articles_from_file
content = Extractor(extractor='ArticleExtractor', url=url).getText()
File "/usr/local/lib/python2.7/site-packages/boilerpipe/extract/__init__.py", line 36, in __init__
connection = urllib2.urlopen(request)
File "/usr/local/lib/python2.7/urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "/usr/local/lib/python2.7/urllib2.py", line 391, in open
response = self._open(req, data)
File "/usr/local/lib/python2.7/urllib2.py", line 409, in _open
'_open', req)
File "/usr/local/lib/python2.7/urllib2.py", line 369, in _call_chain
result = func(*args)
File "/usr/local/lib/python2.7/urllib2.py", line 1173, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/usr/local/lib/python2.7/urllib2.py", line 1148, in do_open
raise URLError(err)
URLError: <urlopen error [Errno 4] Interrupted system call>