suppress/redirect stderr when calling python webrowser

与世无争的帅哥 提交于 2019-12-23 09:32:18

问题


I have a python program that opens several urls in seperate tabs in a new browser window, however when I run the program from the command line and open the browser using

webbrowser.open_new(url)

The stderr from firefox prints to bash. Looking at the docs I can't seem to find a way to redirect or suppress them

I have resorted to using

browserInstance = subprocess.Popen(['firefox'], stdout=log, stderr=log)

Where log is a tempfile & then opening the other tabs with webbrowser.open_new.

Is there a way to do this within the webbrowser module?


回答1:


What is webbrowser.get() giving you?

If you do

 webbrowser.get('firefox').open(url)

then you shouldn't see any output. The webbrowser module choses to leave stderr for some browsers - in particular the text browsers, and then ones where it isn't certain. For all UnixBrowsers that have set background to True, no output should be visible.




回答2:


What about sending the output to /dev/null instead of a temporary file?




回答3:


I think Martin is right about Unix systems, but it looks like things are different on Windows. Is this on a Windows system?

On Windows it looks like webbrowser.py is either going to give you a webbrowser.WindowsDefault browser, which opens the url using

os.startfile(url)

or if Firefox is present it's going to give you a webbrowser.BackgroundBrowser, which starts the browser on Windows using:

p = subprocess.Popen(cmdline)

It looks like only Unix browsers have the ability to redirect stderr in the webbrowser module. You should be able to find out what browser type you're getting by doing

>>> webbrowser.get('firefox')

In a Python interactive console.



来源:https://stackoverflow.com/questions/1352361/suppress-redirect-stderr-when-calling-python-webrowser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!