I have an annoying problem when using machine learning library PyML. PyML uses libsvm to train the SVM classifier. The problem is that libsvm outputs some text to standard output. But because that is outside of Python I cannot intercept it. I tried using methods described in problem Silence the stdout of a function in Python without trashing sys.stdout and restoring each function call but none of those help.
Is there any way how to do this. Modifying PyML is not an option.
Open
/dev/null
for writing, useos.dup()
to copy stdout, and useos.dup2()
to copy your open/dev/null
to stdout. Useos.dup2()
to copy your copied stdout back to the real stdout after.Dave Smith gave a wonderful answer to that on his blog. Basically, it wraps Ignacio's answer nicely:
Now, you can surround any function that garbles unwanted noise into stdout like this:
For Python 3 you can use:
I had the same problem and fixed it like that: