Redirecting stdout to nothing in python
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
Redirect stdout to a file in Python?
If you want to do the redirection within the Python script, setting sys.stdout
to a file object does the trick:
# for python3
import sys
with open('file', 'w') as sys.stdout:
print('test')
A far more common method is to use shell redirection when executing (same on Windows and Linux):
$ python3 foo.py > file
How to output a single print statement out of many and redirect the rest to null in python
Perhaps set sys.stdout
to /dev/null
during the timeit
?
Try it online!
if __name__ == "__main__":
from timeit import timeit
import sys
matrix = [[10, 20, 30, 40], [15, 15, 25, 55], [17, 19, 17, 18], [1, 8, 9, 2]]
stdout = sys.stdout
with open('/dev/null', 'w') as sys.stdout:
t = timeit(lambda: method1(4, 4, matrix), number=10000)
sys.stdout = stdout
print(t) # <-- 3
Temporarily Redirect stdout/stderr
To solve the issue that some function might have cached sys.stdout
stream as a local variable and therefore replacing the global sys.stdout
won't work inside that function, you could redirect at a file descriptor level (sys.stdout.fileno()
) e.g.:
from __future__ import print_function
import os
import sys
def some_function_with_cached_sys_stdout(stdout=sys.stdout):
print('cached stdout', file=stdout)
with stdout_redirected(to=os.devnull), merged_stderr_stdout():
print('stdout goes to devnull')
some_function_with_cached_sys_stdout()
print('stderr also goes to stdout that goes to devnull', file=sys.stderr)
print('stdout is back')
some_function_with_cached_sys_stdout()
print('stderr is back', file=sys.stderr)
stdout_redirected()
redirects all output for sys.stdout.fileno()
to a given filename, file object, or file descriptor (os.devnull
in the example).
stdout_redirected()
and merged_stderr_stdout()
are defined here.
Can I redirect the stdout into some sort of string buffer?
from cStringIO import StringIO # Python3 use: from io import StringIO
import sys
old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()
# blah blah lots of code ...
sys.stdout = old_stdout
# examine mystdout.getvalue()
Python-Undoing stdout redirect
import os
import sys
f = open(os.devnull, 'w')
x = sys.stdout # save sys.stdout
sys.stdout = f
print("a")
sys.stdout = x # re-assign sys.stdout
print("b") # print 'b'
Send `exec()` output to another stream without redirecting stdout
If you're dead set on having a single process, then depending on how willing you are to dive into obscure C-level features of the CPython implementation, you might try looking into subinterpreters. Those are, as far as I know, the highest level of isolation CPython provides in a single process, and they allow things like separate sys.stdout
objects for separate subinterpreters.
Can I redirect all output to /dev/null from within python?
import sys
old_stdout, old_stderr = sys.stdout, sys.stderr
sys.stdout = open('/dev/null', 'w')
sys.stderr = open('/dev/null', 'w')
Related Topics
Python - Using Pandas Structures with Large CSV(Iterate and Chunksize)
Correct Way to Define Class Variables in Python
Python Method for Reading Keypress
Inheritance of Private and Protected Methods in Python
How to Add a Calculated Field to a Django Model
How to Run All Python Unit Tests in a Directory
Importing Pyspark in Python Shell
Popen with Conflicting Executable/Path
How to Plot Nan Values as a Special Color with Imshow in Matplotlib
Read from a Log File as It's Being Written Using Python
Pygame Image Transparency Confusion
Update Row Values Where Certain Condition Is Met in Pandas
Replacing Text in a File with Python
Compulsory Usage of If _Name_=="_Main_" in Windows While Using Multiprocessing
Stop/Start/Pause in Python Matplotlib Animation