How to get the version of a file

Miki miki.tebeka at
Tue Apr 15 05:48:28 CEST 2008

Hello J,

> Does anyone know how to get the version of an application on OS X (i.e.
> the version string that appears in the "Version" field in the "Get Info"
> window for an application)?
> I'm running OS 10.4.11, python 2.5.

#!/usr/bin/env python

from xml.etree.cElementTree import iterparse
from os.path import isfile

def get_version(app):
    info_file = "/Applications/" % app
    if not isfile(info_file):
        raise KeyError("No version file found")

    get = 0
    for event, element in iterparse(open(info_file)):
        if get:
            return element.text

        if (element.tag == "key") and (element.text ==
            get = 1

    raise KeyError("Can't find CFBundleVersion key")

if __name__ == "__main__":
    from sys import argv

    print get_version(argv[1])

Miki <miki.tebeka at>

More information about the Python-list mailing list