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

Mitchell L Model MLMDev at comcast.net
Wed Jan 27 17:00:45 EST 2010


On Jan 27, 2010, at 3:31 PM, Timur Tabi wrote:

> On Wed, Jan 27, 2010 at 12:29 PM, Mitchell L Model  
> <MLMDev at comcast.net> wrote:
>
>> I had some discussions with the Python documentation writers that  
>> led to the
>> following note being included in the Python 3.1 library  
>> documentation for
>> webbrowser.open: "Note that on some platforms, trying to open a  
>> filename
>> using this function, may work and start the operating system’s  
>> associated
>> program. However, this is neither supported nor portable."
>
> Then they should have renamed the API.  I appreciate that they're
> finally documenting this, but I still think it's a bunch of baloney.

I agree, but I am pretty sure that, based on the discussions I had  
with the Python
documenters and developers, that there's no hope of winning this  
argument.
I suppose that since a file: URL is not, strictly speaking, on the  
web, that it
shouldn't be opened with a "web" browser. It's just that the "web"  
part of
"web browser" became more or less obsolete a long time ago since there
are so many more ways of using browsers and so many more things they can
do then just browse the web. So if you interpret the name "webbrowser"  
to mean
that it browses the web, as opposed to files, which means going  
through some
kind of server-based protocol, the module does what it says. But I  
still like
the idea of using it to open files, especially when I want the file to  
be opened
by its associated application and not a browser.

>
>> You can control which browser opens the URL by using webbrowser.get  
>> to
>> obtain a controller for a particular browser, specified by its  
>> argument,
>> then call the open method on the controller instead of the module.
>
> How can I know which controller (application) the system will use when
> it opens an http URL?  I depend on webbrowser.open('http') to choose
> the best web browser on the installed system.  Does webbrowser.get()
> tell me which application that will be?

webbrowser.get() with no arguments gives you the default kind of
browser controller, just as if you had used webbrowser.open()
directly.

>
>> For opening files reliability and the ability to pick a particular  
>> program
>> (browser or otherwise) to open it with you might have to resort to  
>> invoking
>> a command line via subprocess.Popen.
>
> But that only works if I know which application to open.

Aha. You could use subprocess to specify the application from within  
your Python code,
but not to indicate "the user's default browser", unless the platform  
has a command for that.
On OS X, for instance, the command line:
	open file.html
opens file.html with the application the user has associated with html  
files, whereas
	open -a safari file.html
will open it with Safari even if the user has chosen Firefox for html  
files. There's
stuff like this for Windows, I suppose, but hardly as convenient. And  
I think that
Linux environments are all over the place on this, but I'm not sure.

webbrowser.get() returns a control object of the default class for the  
user's environment --
the one that means "use the default browser" so it won't help.


More information about the Python-list mailing list