stdout, stderr redirection in python

hacker_compDuring the development of google-cli, I faced an issue with debug and error messages from Firefox as well as Chromium based browsers. When you launch any URL in the browser using family of APIs, the following show up on the terminal:

// Firefox
(process:8748): GLib-CRITICAL **: g_slice_set_config: assertion 'sys_page_size == 0' failed

// Chromium
Created new window in existing browser session.
[1:2:1013/] RawChannel read error (connection broken)

I checked in several forums including StackOverflow but couldn’t get a complete solution that works for both the browser families. On trial and error, I found one way to hide these warnings and errors is to redirect stdout and stderr to /dev/null. Here’s the OS independent code snippet:

import os

    def open(self):
        _stderr = os.dup(2)
        _stdout = os.dup(1)
        fd =, os.O_RDWR)
        os.dup2(fd, 2)
        os.dup2(fd, 1)
            os.dup2(_stderr, 2)
            os.dup2(_stdout, 1)

Now the warnings and error do not appear on the console anymore.

2 thoughts on “stdout, stderr redirection in python”

Leave a Reply

Your email address will not be published. Required fields are marked *