Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress console output in Python?

Tags:

python

sdl

pygame

I'm using Pygame/SDL's joystick module to get input from a gamepad. Every time I call its get_hat() method it prints to the console. This is problematic since I use the console to help me debug and now it gets flooded with SDL_JoystickGetHat value:0: 60 times every second. Is there a way I can disable this? Either through an option in Pygame/SDL or suppress console output while the function calls? I saw no mention of this in the Pygame documentation.

edit: This turns out to be due to debugging being turned on when the SDL library was compiled.

like image 589
tankadillo Avatar asked Jan 24 '10 02:01

tankadillo


People also ask

How do you suppress print function in Python?

If you don't want that one function to print, call blockPrint() before it, and enablePrint() when you want it to continue. If you want to disable all printing, start blocking at the top of the file.

How do you remove output in Python?

system('cls') to clear the screen. The output window will print the given text first, then the program will sleep for 4 seconds and then screen will be cleared and program execution will be stopped.

What is console output in Python?

The visual display provides console output and the keyboard provides "console input." The simplest way to write to the console or visual display is python's print function.


1 Answers

Just for completeness, here's a nice solution from Dave Smith's blog:

from contextlib import contextmanager import sys, os  @contextmanager def suppress_stdout():     with open(os.devnull, "w") as devnull:         old_stdout = sys.stdout         sys.stdout = devnull         try:               yield         finally:             sys.stdout = old_stdout 

With this, you can use context management wherever you want to suppress output:

print("Now you see it") with suppress_stdout():     print("Now you don't") 
like image 76
charleslparker Avatar answered Oct 18 '22 13:10

charleslparker