I really need webbrowser.open('file://') to open a web browser

Paul Boddie paul at boddie.org.uk
Sat Jan 16 22:43:55 CET 2010


On 15 Jan, 21:14, Timur Tabi <ti... at freescale.com> wrote:
> After reading several web pages and mailing list threads, I've learned
> that the webbrowser module does not really support opening local
> files, even if I use a file:// URL designator.  In most cases,
> webbrowser.open() will indeed open the default web browser, but with
> Python 2.6 on my Fedora 10 system, it opens a text editor instead.  On
> Python 2.5, it opens the default web browser.

The webbrowser module seems to have changed. For example, in the
Python 2.5 standard library, it uses gconftool to query the GNOME
registry and get the preferred browser, whereas in the Python 2.6
standard library, it appears to use gnome-open instead (but only in a
GNOME environment). For KDE, there's a KDE-specific usage of kfmclient
in the 2.6 library. See here for more:

http://svn.python.org/view/python/tags/r254/Lib/webbrowser.py?revision=67917&view=markup
http://svn.python.org/view/python/tags/r264/Lib/webbrowser.py?revision=75707&view=markup

> This is a problem because my Python script creates a local HTML file
> and I want it displayed on the web browser.

Generally, the desktop-specific tools should know that a browser is
the appropriate application for an HTML file, and testing with both
xdg-open, gnome-open and "kfmclient openURL" seems to open browsers on
HTML files (using file:///...) for me (using KDE, Kubuntu 8.04). Of
course, this depends on the settings in use on your desktop, but it
should be noted that using "kfmclient exec" could have the effect you
describe.

> So is there any way to force webbrowser.open() to always use an actual
> web browser?

Not that I'm aware of. Sadly, standardisation of applications and
services - having a command which can open a particular class of
application (such as "e-mail reader", "Web browser") - seems to be
absent from the free desktop arena, although I do recall there being a
preferred applications dialogue in KDE, at least. Maybe this
information is exposed somehow, and maybe I'll incorporate such stuff
into the desktop package eventually:

http://pypi.python.org/pypi/desktop

Note that the desktop package concerns itself precisely with opening
files in text editors if that's how the user has configured their
desktop, whereas the webbrowser module should really only use a Web
browser, obviously.

Paul



More information about the Python-list mailing list