How to suppress all the print info from the Python Script?
<p>Is there an easy way to suppress all the print info from the python script globally ?</p>

<p>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.</p>

<p>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. </p>

<p>Tried simply like, </p>

<pre><code>if sys.argv[1]:
  debug=1
else:
  debug=0

if debug:
  print "Enabled debugging" 
</code></pre>

<p>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.</p>

<p>Thanks</p>

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

<pre><code>import os
import sys
f = open(os.devnull, 'w')
sys.stdout = f
</code></pre>

python python-2.7