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

Discussion in 'Python' started by Timur Tabi, Jan 15, 2010.

  1. Timur Tabi

    Timur Tabi Guest

    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.

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

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

    --
    Timur Tabi
    Linux kernel developer at Freescale
    Timur Tabi, Jan 15, 2010
    #1
    1. Advertising

  2. On Jan 15, 8:14 pm, Timur Tabi <> 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.
    >
    > This is a problem because my Python script creates a local HTML file
    > and I want it displayed on the web browser.
    >
    > So is there any way to force webbrowser.open() to always use an actual
    > web browser?
    >
    > --
    > Timur Tabi
    > Linux kernel developer at Freescale


    Might not be useful, but trying open_new_tab() on...

    Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
    [GCC 4.3.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import webbrowser as wb
    >>> x = wb.get()
    >>> x.basename

    'gnome-open'

    When attempting to use open_new_tab(), I get:

    file:///home/jon/blahblah.html - opens in Firefox
    file:///home/jon/blahblah.txt - opens in gedit

    Jon.
    Jonathan Temple, Jan 16, 2010
    #2
    1. Advertising

  3. Timur Tabi

    Jon Clements Guest

    On Jan 16, 5:08 pm, Jonathan Temple <> wrote:
    > On Jan 15, 8:14 pm, Timur Tabi <> 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.

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

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

    >
    > > --
    > > Timur Tabi
    > > Linux kernel developer at Freescale

    >
    > Might not be useful, but trying open_new_tab() on...
    >
    > Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
    > [GCC 4.3.3] on linux2
    > Type "help", "copyright", "credits" or "license" for more information.>>> import webbrowser as wb
    > >>> x = wb.get()
    > >>> x.basename

    >
    > 'gnome-open'
    >
    > When attempting to use open_new_tab(), I get:
    >
    > file:///home/jon/blahblah.html - opens in Firefox
    > file:///home/jon/blahblah.txt  - opens in gedit
    >
    > Jon.


    Err, I'd just like to mention that "Jonathan Temple" did not post that
    message: He was checking his Google Mail on my machine, and when I
    went to send my post, it got a little confused.

    Cheers,

    Jon.
    Jon Clements, Jan 16, 2010
    #3
  4. Timur Tabi

    Paul Boddie Guest

    On 15 Jan, 21:14, Timur Tabi <> 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
    Paul Boddie, Jan 16, 2010
    #4
  5. Timur Tabi

    Timur Tabi Guest

    On Sat, Jan 16, 2010 at 3:43 PM, Paul Boddie <> wrote:

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


    I'm using Gnome, and I have HTML files associated with Firefox.
    However, my default web browser is Seamonkey, and when I do
    webbrowser.open('http://...'), it opens that URL in Seamonkey, not
    Firefox. So if there is some Gnome association between an .html file
    and a text editor, I don't know where it is defined.

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


    I would be sympathetic to this problem if the API were called
    desktop.open(...). But it's called webbrowser.open(), so it has to be
    certain that a web browser is being at all times. IMHO, any other
    behavior is a bug.

    --
    Timur Tabi
    Linux kernel developer at Freescale
    Timur Tabi, Jan 18, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. robin
    Replies:
    0
    Views:
    379
    robin
    May 18, 2006
  2. krishnakant Mane

    problem opening html file with webbrowser.open

    krishnakant Mane, Oct 29, 2007, in forum: Python
    Replies:
    2
    Views:
    238
    Stefan Behnel
    Oct 29, 2007
  3. krishnakant Mane
    Replies:
    0
    Views:
    276
    krishnakant Mane
    Nov 12, 2007
  4. Replies:
    30
    Views:
    962
    Arne Vajhøj
    Feb 10, 2008
  5. Mitchell L Model
    Replies:
    1
    Views:
    478
    Paul Boddie
    Jan 27, 2010
Loading...

Share This Page