Call the operating system to open the URL?

What can I use to call the OS to open the URL in any browser that the user has by default? Do not worry about compatibility between OS; if it works on Linux, that's enough for me!

+46
python url system-calls operating-system
Nov 18 '10 at 16:16
source share
5 answers

Here's how to open the default browser for a user with a given URL:

import webbrowser webbrowser.open(url[, new=0[, autoraise=True]]) 

Here is the documentation about this functionality. This is part of Python stdlibs:

http://docs.python.org/library/webbrowser.html

I have successfully tested this on Linux, Ubuntu 10.10.

+69
Nov 18 '10 at 16:19
source share

Personally, I really would not use the webbrowser module.

This is a complex sniffing mess for certain browsers that will not find the user's default browser if they have more than one installed, and will not find the browser if he does not know his name (for example, Chrome).

It's best on Windows just to use the os.startfile function, which also works with the url. In OS X, you can use the open system command. Linux has xdg-open , a standard freedesktop.org command supported by GNOME, KDE, and XFCE.

 if sys.platform=='win32': os.startfile(url) elif sys.platform=='darwin': subprocess.Popen(['open', url]) else: try: subprocess.Popen(['xdg-open', url]) except OSError: print 'Please open a browser on: '+url 

This will provide the best user experience on major platforms. Perhaps you could return to webbrowser on other platforms. Although, most likely, if you are in an obscure / unusual / embedded OS, where none of the above tasks has a chance on a webbrowser .

+26
Nov 18 2018-10-18
source share

You can use the webbrowser module.

 webbrowser.open(url) 
+3
Nov 18 '10 at 16:19
source share

Then, how about mixing the @kobrien and @bobince codes up:

 import subprocess import webbrowser import sys url = 'http://test.com' if sys.platform == 'darwin': # in case of OS X subprocess.Popen(['open', url]) else: webbrowser.open_new_tab(url) 
+3
May 13 '13 at 4:07
source share

Take a look at the web browser module .

+2
Nov 18 '10 at 16:19
source share



All Articles