python提示符错误_Python:禁止从命令行转到错误?

When I try to execute a python program from command line, it gives the following error. These errors do not cause any problem to my ouput. I dont want it to be displayed in the commandline

Traceback (most recent call last):

File "test.py", line 88, in

p.feed(ht)

File "/usr/lib/python2.5/HTMLParser.py", line 108, in feed

self.goahead(0)

File "/usr/lib/python2.5/HTMLParser.py", line 148, in goahead

k = self.parse_starttag(i)

File "/usr/lib/python2.5/HTMLParser.py", line 226, in parse_starttag

endpos = self.check_for_whole_start_tag(i)

File "/usr/lib/python2.5/HTMLParser.py", line 301, in check_for_whole_start_tag

self.error("malformed start tag")

File "/usr/lib/python2.5/HTMLParser.py", line 115, in error

raise HTMLParseError(message, self.getpos())

HTMLParser.HTMLParseError: malformed start tag, at line 319, column 25

How could I suppress the errors?

解决方案

Doesn't catching HTMLParseError work for you? If test.py is the name of your python file, it's propagated up to there, so it should.

Here's an example how to suppress such an error. You might want to tweak it a bit to match your code.

try:

# Put parsing code here

except HTMLParseError:

pass

You can also just suppress the error message by redirecting stderr to null, like Ignacio suggested. To do it in code, you can just write the following:

import sys

class DevNull:

def write(self, msg):

pass

sys.stderr = DevNull()

However, this is probably not be what you want, because from your error it looks like the script execution is stopped, and you probably want it to be continued.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值