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:

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

Write a Reply or Comment

Your email address will not be published.