Music files come, music files go. And after some time your iTunes library is sprinkled with cases of

The song “Monsieur Cannibale” could not be used because the original file could not be found. Would you like to locate it?

Being annoyed by this and having no idea how "healthy" my library was, I decided to delve in the corners of ~/Music/iTunes/iTunes Library.xml. After considering some approaches with XML parsing libraries, I stumbled on this nice solution which directly hooks into Mac OSX's frameworks with the PyObjC, a Python-ObjC bridge.

Long story short, I hacked this little script together to scan through the tracks in my iTunes library and complain about local files it can't find:

import sys
import os
import Foundation

def scan_itunes_library(library_file):

    # Load iTunes library
    db = Foundation.NSDictionary.dictionaryWithContentsOfFile_(library_file)

    # Check track info.
    for track in db[u'Tracks'].itervalues():

        if u'Location' not in track:
            print 'No location info in track info', track
            continue

        # Resolve location
        nsurl = Foundation.NSURL.URLWithString_(track[u'Location'])

        # Check local file paths
        if nsurl.scheme() == u'file' and nsurl.host() == u'localhost':
            if not os.path.exists(nsurl.path()):
                print "Location does not exist:", nsurl.path(), 'from track', track
        else:
            print "Don't know how to check", nsurl


if __name__ == '__main__':
    scan_itunes_library(library_file=sys.argv[1])

Run this script with the path to your iTunes Library XML file (~/Music/iTunes/iTunes Library.xml typically) as argument.