How to get the version of a file

Miki miki.tebeka at gmail.com
Mon Apr 14 23:48:28 EDT 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/%s.app/Contents/Info.plist" % 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 ==
"CFBundleVersion"):
            get = 1

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

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

    print get_version(argv[1])

HTH,
--
Miki <miki.tebeka at gmail.com>
http://pythonwise.blogspot.com



More information about the Python-list mailing list