Redirect stdout to a file
Your script produces some output to the standard output and you want to redirect it to a file. For instance, you want to suppress the output to
/dev/null. However, you want to do this redirection from your script and not in the shell.
Add the following lines to the beginning of your script (before the first
old_stdout = sys.stdout sys.stdout = open(os.devnull, 'w')
Of course, instead of
os.devnull you can specify a normal text file too. To restore printing to stdout, point
In this thread you will see a solution for redirecting the stdout to a string.