In order to prevent this from being a X Y problem, I will tell you the problem, and the proposed solutions that I cant figure out how to do.
I am trying to call the webbrowser module in a script.
import webbrowser
webbrowser.open_new_tab('http://stackoverflow.com')
Which is quite awesome. However, I want to be able to squash the input that it gives me. On my current machine(ubuntu) my browser(firefox) spits out a bunch of crap through stderr. I would like to be able to call webbrowser and eliminate the text from showing up.
What I have tried:
1 Use popen so that I can pipe the process to nothing.
from subprocess import Popen, PIPE
Popen(['python -m webbrowser \'' + url + '\''], stderr=PIPE,
stdout=PIPE, shell=True)
Why this doesn't work
This actually works great in non-headless mode! When I use this in a ssh terminal (headless mode) the subprocess aspect doesn't allow me to see the headless terminal browser that is an oh so cool feature of webbrowser.
2 Redirect stderr
I cant just redirect stderr in python because the python script typically ends before the web browser is finished printing, so the process of the webbrowser continues to print in the terminal after the script has ended.
I have also tried: Suppressing output of module calling outside library with no luck
I cant use the suggestion in: suppress/redirect stderr when calling python webrowser because I want webbrowser
to choose the default browser.
Possible solutions (only one of these would suffice):
Is there a way to know if I am in headless mode? If I could know that I was in an ssh/headless mode, I could use one method for headless mode, and the other for non headless.
Is there a way to do webbrowser.get('default').open() so that webbrowser doesnt give me the junk?
Is there a way to "pipe" the output of the browser without using subprocess?
Anything else that will solve the problem...