Temporarily Suppress Console Output in Python

Have you ever wanted to temporarily suppress console output in Python? But you really want it to be temporary, even if an exception happens. Maybe you are calling into some idiot’s library who spams your console. Yeah, that’s happened to me before (maybe I was the idiot–I’m not tellin’).

Here’s a handy way to suppress stdout temporarily in your program:

First, slap this code into your project:

Use it like this:

3 comments to “Temporarily Suppress Console Output in Python”

You can leave a reply or Trackback this post.
  1. can this be used for a HTTP GET output?

  2. http://Johnny says: -#1

    Hi Dave,

    Thanks very much, actually I was looking this exactly. In this case, I am also responsible for the unnecessary output, but that is because I am running other tools into my python tool/script.

    I just want to make you one question, how did you figure out?

    How did you build this code? Where did you take those libraries?

    ( Sorry, I know I said one question but this really interest me :P )

Write a Reply or Comment

Your email address will not be published.