Opening Files in an Editor from Python

For my latest attempt at a blog I’m using Github’s built-in blogging system. Using a git repo as the storage, Markdown for the formatting, and a text-editor for composition seems like a really nice combination. In fact, I’m optimistic that I might make it past the three-post-wall, which up until now, has been the most of I’ve ever posted to any of my ill-fated proto-blogs.

Given this dubious record, I’ve decided to remove as many barriers to posting as possible. And, as nice as Jekyll makes it—just create a new file under the _posts directory—I wanted to be able to kick off a new entry with a single command, something like:

newpost My Snazzy New Blog Entry

I wanted this to: * Create the properly named file under the _posts directory * Drop in the heading text automatically * Open the new file in my default text editor

Of these, only the third was interesting. It turns out opening a file in a text-editor isn’t as clear cut as I thought. There are really two routes you can go:

The route I decided to go with was using the system’s text-file handler, which in my case is MacVim. The one trick to getting this right is to note that Macs and other UNIXes have two different ways for accomplishing this: Macs use the open(1) command whereas other POSIX OSes use xdg-open(1). The final code, taken from StackOverflow with a small modification to handle the Mac case properly:

import platform, os, subprocess

def open_in_editor(filepath):
    # NOTE: Macs are 'posix' but don't have xdg-open
    if platform.platform().startswith('Darwin'):
        subprocess.call(('open', filepath))
    elif os.name == 'nt':
        subprocess.call(('start', filepath), shell=True)
    elif os.name == 'posix':
        subprocess.call(('xdg-open', filepath))
    else:
        raise Exception('Unrecognized OS %s' % os.name)
Posted on 09 June 2011.
blog comments powered by Disqus