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

>>> 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.

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

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/")

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???

How to open default text editor?

Using Linux and Python 2.6

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.

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)

This does open file in Firefox in my Windows computer:

>>> import os
>>> os.startfile('C:\VALUEADD\VALUEADD.HTM')

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")
commented: thanks +10

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
>>>

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.

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
>>>

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???

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)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.