Python: Suppressing errors from going to commandline?
Asked Answered
C

5

8

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?

Cutlet answered 8/5, 2011 at 6:9 Comment(0)
S
12

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.

Satiable answered 8/5, 2011 at 6:39 Comment(5)
When I gave nothing as Except argument, It worked. But when I gave except HTMLParseError: am getting error NameError: name 'HTMLParseError' is not defined. So how could I know the exact error thing? Cutlet
Then you should either catch HTMLParser.HTMLParseError, or import it explicitly with from HTMLParser import HTMLParseError.Satiable
Oh thanks. I couldnt understand it from the error message. But when I gave from HTMLParser import HTMLParseError it worked. But when I gave HTMLParser.HTMLParseError, it gave the error AttributeError: class HTMLParser has no attribute 'HTMLParseError'. Why is it so?Cutlet
You've probably had a from HTMLParser import HTMLParser in your code (so the class HTMLParser masked the module HTMLParser). You didn't post the code here, so I was really pushing in the dark. Anyway, I suggest you to read the Python tutorial about modules and exceptionsSatiable
Yes I had. So whats the problem with that?Cutlet
R
8

Redirect stderr to /dev/null.

python somescript.py 2> /dev/null
Rothmuller answered 8/5, 2011 at 6:14 Comment(5)
Is there a programming way of handling it. HTMLParser gives error due to malformed tags. But it doesnt cause any problem for me. Is there any method using try/catchCutlet
Depends. Are you writing the program?Rothmuller
Just a guess, you could put the offending code inside of a try/except block.Interlock
Yes I am writing the program. I want to mute the errors since it wont affect the output at allCutlet
Then yes, you would catch the exceptions as explained in the Python tutorial.Rothmuller
O
7

In python 3, @Boaz Yaniv's answer can be simplified as

sys.stderr = object

since every class in python3 is inherited from Object, so technically this would work, at least I've tried it by myself in python 3.6.5 environment.

Oster answered 31/5, 2018 at 4:19 Comment(1)
you need to import sys to run thisKhiva
A
7

Here is a more readable, succinct solution for handling errors that are safe to ignore, without having to resort to the typical try/except/pass code block.

from contextlib import suppress

with suppress(IgnorableErrorA, IgnorableErrorB):
    do_something()
Ansilma answered 26/8, 2020 at 8:4 Comment(0)
A
0

There are a lot of amazing answers and I recently had a requirement that I want to suppress the error from printing in the terminal and also inputs should be enabled and the easiest way I found was below.

import sys
def supressErrors(*args):
    pass

suppressionHandle = sys.stderr
sys.stderr = supressErrors()
print("Testing print 1/0")
print(1/0) #Nothing will be printed in the terminal

sys.stderr = suppressionHandle
print(1/0) # You can see an exception in the terminal
Austere answered 28/6 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.