views:

92

answers:

3

I'm writing a python script that generates html file. Every time I run this script I'd like at the end to open default system browser for this file. It's all in OS X environment.

What python code can launch Safari/Firefox/whatever is system default html viewer and open given file? subprocess.call doesn't seem to do the trick.

+1  A: 

Do you know about the open command in Mac OS X? I think you can solve your problem by calling it from Python.

man open for details:

The open command opens a file (or a directory or URL), just as if you had double-clicked the file's icon. If no application name is specified, the default application as determined via LaunchServices is used to open the specified files.

Pascal Cuoq
Just use "import webbrowser" and "webbrowser.open()", on OSX it does the same as the open command from OSX itself. So opening a pdf will open it in preview and not in your browser (despite the module's name).
Reinout van Rees
+3  A: 

What python code can launch Safari/Firefox/whatever is system default html viewer and open given file?

There is a webbrowser module in python, try this:

import webbrowser
webbrowser.open('file://%s' % path)

This will open a new tab in the default browser. There are methods to open a new tab, new window and other options.

Nadia Alramli
I love this module so much.
jathanism
Yes, use the webbrowser module. Note that it actually functions the same way as "open" on osx that simply opens the default application, whether it is the webbrowser or not. So webbrowser.open('my.pdf') probably opens your pdf reader, not the webbrowser. A really luxuriously great module!
Reinout van Rees
Thanks! Works like a charm.
Marcin
A: 

import ic

ic.launchurl('file:///somefile.html')

prime_number