What can I use to call the OS to open a URL in whatever browser the user has as default? Not worried about cross-OS compatibility; if it works in linux thats enough for me!
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Here is how to open the user's default browser with a given url:
Here is the documentation about this functionality. It's part of Python's stdlibs:
http://docs.python.org/library/webbrowser.html
I have tested this successfully on Linux, Ubuntu 10.10.
Have a look at the webbrowser module.
Then how about mixing codes of @kobrien and @bobince up:
Personally I really wouldn't use the
webbrowser
module.It's a complicated mess of sniffing for particular browsers, which will won't find the user's default browser if they have more than one installed, and won't find a browser if it doesn't know the name of it (eg Chrome).
Better on Windows is simply to use the
os.startfile
function, which also works on a URL. On OS X, you can use theopen
system command. On Linux there'sxdg-open
, a freedesktop.org standard command supported by GNOME, KDE and XFCE.This will give a better user experience on mainstream platforms. You could fall back to
webbrowser
on other platforms, perhaps. Though most likely if you're on an obscure/unusual/embedded OS where none of the above work, chances arewebbrowser
will fail too.You can use the webbrowser module.