Reputation: 1143
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 the user runs I don't want the script to print all the infos.
So only when I pass a command line argument like debug=1 or something like that, the print needs to provide the info else it shouldn't print.
Tried simply like,
if sys.argv[1]:
debug=1
else:
debug=0
if debug:
print "Enabled debugging"
But for this I need to include the if condition everywhere, instead is there any easy way to shutdown the print info globally ? Share in your comments.
Thanks
Upvotes: 1
Views: 1656
Reputation: 1877
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
Upvotes: 5
Reputation: 599550
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.
Upvotes: 4