NicoCati NicoCati - 3 years ago 118
Python Question

To prevent a function from printing in the batch console in Python

Well, the headline seems to me sufficient.
I use some function that at some points print something in the console.
As I can't modify them, I would like to know if there is a solution to not printing while using these functions.

Thanks a lot !


Answer Source

Yes, you can redirect sys.stdout:

import sys
import os

sys.stdout = open(os.devnull, "w")


sys.stdout = sys.__stdout__

Just replace my_nasty_function with your actual function.

EDIT: Now should work on windows aswell and makes use of sys.__stdout__ instead of a backup variable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download