Skip to content Skip to sidebar Skip to footer

How To Suppress All The Print Info From The Python Script?

Is there an easy way to suppress all the print info from the python script globally ? Have a scenario where I had put lot of print info in the script for debug purpose, but when th

Solution 1:

You will have to redirect stdout to /dev/null. Below is the OS agnostic way of doing it.

import os
import sys
f = open(os.devnull, 'w')
sys.stdout = f

Solution 2:

This is why you should use the built-in logging library rather than writing print statements everywhere.

With that, you can call logger.debug() wherever you need to, and configure at application level whether or not the debug logs are actually output.

Post a Comment for "How To Suppress All The Print Info From The Python Script?"