I'm using the following code to hide stderr on Linux/OSX for a Python library I do not control that writes to stderr by default:
f = open("/dev/null","w") zookeeper.set_log_stream(f) Is there an easy cross platform alternative to /dev/null? Ideally it would not consume memory since this is a long running process.
You write to /dev/null every time you use it in a command such as touch file 2> /dev/null. You read from /dev/null every time you empty an existing file using a command such as cat /dev/null > bigfile or just > bigfile. Because of the file's nature, you can't change it in any way; you can only use it.
The null device is typically used for disposing of unwanted output streams of a process, or as a convenient empty file for input streams. This is usually done by redirection. The /dev/null device is a special file, not a directory, so one cannot move a whole file or directory into it with the Unix mv command.
Whatever you write to /dev/null will be discarded, forgotten into the void. It's known as the null device in a UNIX system.
How about os.devnull ?
import os f = open(os.devnull,"w") zookeeper.set_log_stream(f)
class Devnull(object): def write(self, *_): pass zookeeper.set_log_stream(Devnull()) Opening os.devnull is fine too of course, but this way every output operation occurs (as a noop) "in process" -- no context switch to the OS and back, and also no buffering (while some buffering is normally used by an open) and thus even less memory consumption.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With