Redirecting stdout to "nothing" in python -



Redirecting stdout to "nothing" in python -

i have big project consisting of sufficiently big number of modules, each printing standard output. project has grown in size, there big no. of print statements printing lot on std out has made programme considerably slower.

so, want decide @ runtime whether or not print stdout. cannot create changes in modules there plenty of them. (i know can redirect stdout file considerably slow.)

so question how redirect stdout nil ie how create print statement nothing?

# want this. sys.stdout = none # give error nonetype object not have write method.

currently thought have create class has write method (which nothing) , redirect stdout instance of class.

class dontprint(object): def write(*args): pass dp = dontprint() sys.stdout = dp

is there inbuilt mechanism in python this? or there improve this?

cross-platform:

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

on windows:

f = open('nul', 'w') sys.stdout = f

on linux:

f = open('/dev/null', 'w') sys.stdout = f

python python-2.7

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -