Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Suppressing errors from going to commandline?

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 <module>
    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?

like image 575
user567879 Avatar asked May 08 '11 06:05

user567879


People also ask

How do you stop exceptions in Python?

Using Try Exceptblock to catch the ZeroDivisionError exception and ignore it. In the above code, we catch the ZeroDivisionError exception and use pass to ignore it. So, when this exception happens, nothing will be thrown and the program will just keep running by ignoring the zero number.

What can I use instead of try except Python?

The try block lets you test a block of code for errors. The except block lets you handle the error. The else block lets you execute code when there is no error. The finally block lets you execute code, regardless of the result of the try- and except blocks.

Does raising an error in Python stop execution?

When an exception is raised, no further statements in the current block of code are executed. Unless the exception is handled (described below), the interpreter will return directly to the interactive read-eval-print loop, or terminate entirely if Python was started with a file argument.


2 Answers

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.

like image 187
Boaz Yaniv Avatar answered Nov 27 '22 01:11

Boaz Yaniv


Redirect stderr to /dev/null.

python somescript.py 2> /dev/null
like image 45
Ignacio Vazquez-Abrams Avatar answered Nov 27 '22 00:11

Ignacio Vazquez-Abrams