How to redirect output from 'bpy.ops.*'? When calling operators such as bpy.ops.mesh.remove_doubles() it outputs how many vertices were removed into console.

In some cases its better to silence it, but you may also want to catch it and use it within the script.

How can I redirect or otherwise silence the text from printing into the terminal?
This can be done by temporarily overriding the output using redirect_stdout.

eg:

Short example:

from contextlib import redirect_stdout
import io
stdout = io.StringIO()
with redirect_stdout(stdout):
    bpy.ops.mesh.remove_doubles()


Commented answer with redirect:

# Python module to redirect the sys.stdout
from contextlib import redirect_stdout
# So we can create a file-like object
import io

# our new output
stdout = io.StringIO()

with redirect_stdout(stdout):
    # any printing or operator output within this block
    # will go into 'stdout'
    bpy.ops.mesh.remove_doubles()

# if you like to get the output and use it
# you can read it as if its a regular file.
stdout.seek(0)
output = stdout.read()
del stdout

print("We caught the output, it was %d long!" % len(output))




Note, that this can also be used to re-direct operator reports for your own use, and example of this is a benchmarking tool which extracts output from the draw timer.
https://gitlab.com/ideasman42/blender-fps-bench/blob/master/blender_fps_bench.py#L196