views:

48

answers:

3

I want to open an image in the default browser with Python. I thought it might be as simple as webbrowser.open(path_to_file), but on XP at least that opens the Windows Picture and Fax Viewer instead.

A: 

This is a slightly difficult question to answer with the current information. It would be helpful if you could clarify which browser you are using and what image format you are trying to display.

Without that information, I can provide the following:

Here I will assume that you will be using a Firefox browser and a jpg image.

import os
os.system('"C:\\Program Files\\ Mozilla Firefox\\Firefox.exe" "path_to_file.jpg"')

This works on my WinXP system.

Now to explain the code. the os module in python has some nifty OperatingSystem tools. os.system executes the input string as a command just as you would if you tried to do so from cmd.

Firefox may not be part of the path variables and may therefore need to be called explicitly from where it lives. This is why I have "C:\\Progam Files...".

You will notice that I have two sets of double quotes in the input parameter to os.system This is because the path to firefox and your jpg might have spaces in them and the Windows command line is fussy about this.

Also, you might notice that there are double-backslashes in the double-quotes. This is because in Python, a backslash is an escape character and is used to give special meaning to the following character (for example "\t" is tab, etc). Therefore, in order to get an actual backslash, we need to escape the escaping nature of the backslash and do "\\".

inspectorG4dget
A: 

Well, it should be that simple (in my opinion), but the problem is with how the webbrowser module sets up the default browser on Windows. Because of this, when you type in

webbrowser.open(path_to_file)

what is then called is

os.startfile(url)

which works fine for url's, but for files, it uses the Windows Picture and Fax Viewer unless you've associated some other program with the image file type. Basically, if you use the webbrowser.get() command to get an actual browser it will be fine. Here's a way to do it with internet explorer on Windows (which has been set-up in the webbrowser module to be the hardest to get) :

import os,webbrowser
iexplore = os.path.join(os.environ.get("PROGRAMFILES", "C:\\Program Files"),
                        "Internet Explorer\\IEXPLORE.EXE")
browser = webbrowser.get(iexplore)
browser.open(path_to_file)

other ones like firefox are easier to get:

browser = webbrowser.get("firefox")
Justin Peel
makes sense. would it be a good idea to just use something like "try:firefox try:chrome try:internet_explorer except:default"? i can't think of a better way
Jeff
Well, you can actually change webbrowser's _tryorder variable to tweak which ones are tried first(first element in _tryorder is the first browser tried). However, there are other problems with the webbrowser module. For instance, it currently doesn't try to find chrome or safari and it seems to be unable to find firefox in my Windows 7 (64-bit) distribution (though it did find firefox in Linux for me). Overall, the webbrowser module looks like it needs some work including the above and that Internet Explorer isn't my default browser, but os.startfile() uses IE with a url for me.
Justin Peel
A: 

Looks like that particular installation of Windows is set to use that viewer program as the "default browser" (for information with that content type, at least). To check, what happens when (at Windows' cmd.exe prompt) you type start path_to_file?

If this confirms that this is indeed the installation's choice, then if you want to open that file with some other program then you cannot at the same time want to be using the default browser for that installation (because that viewer, which you apparently do not want to open, is set as the default browser for that file yet -- so you must be wanting to open some other browser instead of the default one!).

If that's indeed the case, I recommend trying webbrowser.get to get a controller for the browser with the given name (e.g. try c = webbrowser.get('windows-default') -- or, if that, as is quite possible, reproduces the behavior you don't want, try get('firefox') instead), then c.open(path_to_file) to open the file in question.

Alex Martelli
"start path_to_file" does open it in the Picture and Fax Viewer. i guess i'll try to manually find the right browser.
Jeff