1,105,169 Community Members

how do I call the default browser from a python program?

Member Avatar
G_S
Junior Poster
142 posts since Mar 2010
Reputation Points: 38 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi people,

the title says it all:

The user manual of my program is an HTML document, and I want the OS's default browser to open it directly once the user clics on help.


The function that the help menu entry invokes is this:

os.popen("/usr/local/firefox/firefox ./html/index.html")

Now this only works on Linux and because I know where the firefox executable is. Is there a way of calling the default browser? And how would I do it on Windows?

Remember that I want the function to call the browser and also tell it what file it should open.

Thanks in advane

Member Avatar
d5e5
Practically a Posting Shark
827 posts since Sep 2009
Reputation Points: 109 [?]
Q&As Helped to Solve: 163 [?]
Skill Endorsements: 2 [?]
 
0
 
>>> import webbrowser
>>> webbrowser.open('/home/david/index.html')
True
>>>

It opens my default browser, Firefox, and displays my index.html file. I think it's supposed to do the same on other platforms.

Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

General way to open associated program, but unfortunately only for Windows is to use os.startfile()

Member Avatar
jcao219
Posting Pro in Training
414 posts since Dec 2009
Reputation Points: 18 [?]
Q&As Helped to Solve: 97 [?]
Skill Endorsements: 1 [?]
 
0
 

To clarify tonyjv's post, let's so for instance to open daniweb in the default browser would be: os.startfile("http://www.daniweb.com/")

Member Avatar
G_S
Junior Poster
142 posts since Mar 2010
Reputation Points: 38 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Let me see if I got this right:

os.startfile("PATH OR URL") would open the defaultbrowser for windows and send it there?

and

webbrowser.open("PATH OR URL") would do the same but for any platform?


can you clarify why they are so different?? Which one is better???

Member Avatar
-ordi-
Junior Poster in Training
91 posts since Dec 2009
Reputation Points: 6 [?]
Q&As Helped to Solve: 11 [?]
Skill Endorsements: 0 [?]
 
0
 

How to open default text editor?

Using Linux and Python 2.6

Member Avatar
d5e5
Practically a Posting Shark
827 posts since Sep 2009
Reputation Points: 109 [?]
Q&As Helped to Solve: 163 [?]
Skill Endorsements: 2 [?]
 
0
 

Let me see if I got this right:

os.startfile("PATH OR URL") would open the defaultbrowser for windows and send it there?

and

webbrowser.open("PATH OR URL") would do the same but for any platform?


can you clarify why they are so different?? Which one is better???

When the os module is compiled on an non-Windows platform it doesn't have a startfile attribute or method. I guess os.startfile works on Windows by checking the Windows registry for associations between the file extension and its default program.

webbrowser.open("PATH OR URL") works on both Windows and Linux, etc., presumably because it does not depend on the Windows registry to discover the default browser. That's all I know about os.startfile and webbrowser.open so far so can't say which is best.

Member Avatar
G_S
Junior Poster
142 posts since Mar 2010
Reputation Points: 38 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Oh, wait, it's not completely solved:

on linux, everything is fine.

on windows, however, webbrowser.open only opens Explorer, even though firefox is the default.

If I try startfile, it does open the associated program as d5e5 explained, but it fails to open the help file (it seems it only works with URL's)

Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

This does open file in Firefox in my Windows computer:

>>> import os
>>> os.startfile('C:\VALUEADD\VALUEADD.HTM')
Member Avatar
Gribouillis
Posting Maven
3,452 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 883 [?]
Skill Endorsements: 18 [?]
Moderator
 
1
 

Oh, wait, it's not completely solved:

on linux, everything is fine.

on windows, however, webbrowser.open only opens Explorer, even though firefox is the default.

If I try startfile, it does open the associated program as d5e5 explained, but it fails to open the help file (it seems it only works with URL's)

To make sure you open firefox in a cross platform way, use webbrowser.get

import webbrowser
webbrowser.get("firefox").open("http://www.python.org")
Member Avatar
d5e5
Practically a Posting Shark
827 posts since Sep 2009
Reputation Points: 109 [?]
Q&As Helped to Solve: 163 [?]
Skill Endorsements: 2 [?]
 
0
 

Oh, wait, it's not completely solved:

on linux, everything is fine.

on windows, however, webbrowser.open only opens Explorer, even though firefox is the default.

If I try startfile, it does open the associated program as d5e5 explained, but it fails to open the help file (it seems it only works with URL's)

I tried the following

>>> import webbrowser
>>> webbrowser.open('c:\Users\David\Programming\Javascript\CHAPTERS\ch 08\index.htm')
True
>>>

on Windows Vista Home Premium and it opened the file in my default browser, which is Chrome. In Linux it opened in Firefox, which is my default browser for Linux. I don't know why webbrowser is using Explorer on your computer when it is not the default. (Maybe your version of Windows or Explorer is different than mine?) Have you tried the following?

>>> webbrowser.get("windows-default").open('c:\Users\David\Programming\Javascript\CHAPTERS\ch 08\index.htm')
True
>>>
Member Avatar
G_S
Junior Poster
142 posts since Mar 2010
Reputation Points: 38 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

OK, I'll try that.

One more question: I want to open a local file with the browser. Is it possible to use arelative path? I wouldn't know where the user places the folder, so the path to the html file has to be relative.

Member Avatar
d5e5
Practically a Posting Shark
827 posts since Sep 2009
Reputation Points: 109 [?]
Q&As Helped to Solve: 163 [?]
Skill Endorsements: 2 [?]
 
0
 

OK, I'll try that.

One more question: I want to open a local file with the browser. Is it possible to use arelative path? I wouldn't know where the user places the folder, so the path to the html file has to be relative.

I tried opening the file using the absolute path, then finding out my current working directory and opening the file with a path relative to it, then changing the cwd to the file location and opening the file by name only. It all works, on Linux anyway.

>>> import webbrowser
>>> webbrowser.open('/home/david/Programming/Python/index.html')
True
>>> import os
>>> os.getcwd()
'/home/david'
>>> webbrowser.open('Programming/Python/index.html')
True
>>> os.chdir('Programming/Python')
>>> webbrowser.open('index.html')
True
>>>
Member Avatar
G_S
Junior Poster
142 posts since Mar 2010
Reputation Points: 38 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

Yes, it works on Linux, but on Windows 7, it's opening iexplorer and telling it to go to http://./html/index.html

Thi is how it works on my windows 7:

* default browser is firefox
* if I write webbrowser.open('./index.html'), it opens explorer and tries to open http://./index.html
* if I write webbrowser.open('C:/ABSOLUTE PATH/index.html'), it does open the default browser (firefox) with the correct url.

What is going on???

Member Avatar
d5e5
Practically a Posting Shark
827 posts since Sep 2009
Reputation Points: 109 [?]
Q&As Helped to Solve: 163 [?]
Skill Endorsements: 2 [?]
 
0
 

I can't explain what is going on in Windows, but from what you say it seems the solution is to have your program get and use the absolute path joined with the filename. The following should work on both platforms:

#!/usr/bin/env python
import webbrowser
import os
abs_path = os.path.abspath('.') #Absolute path of current working directory
filename = os.path.join(abs_path, 'index.html')
print 'Try to open ' + filename
webbrowser.open(filename)
Member Avatar
G_S
Junior Poster
142 posts since Mar 2010
Reputation Points: 38 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

This works wonder. Thanks a lot. Something must be wrong with this poarticular Windows configurations since it doesn't open the default browser but rather explorer all the time. But your code works on Linux and Windows as expected (and somehow manages to open the real default browser).

Tha you all for your help.

Question Answered as of 3 Years Ago by d5e5, pyTony, -ordi- and 2 others
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article