Friday, December 12, 2008

Using python to examine your iTunes library

I recently noticed a bunch of music files were missing from my iTunes music library. Here's a simple python program I write to find which ones were missing... (iTunes tells you, but in a modal dialogue window so you can't look at the list and check your library at the same time)


import plistlib
import urllib

filename = "/Users/matt/Music/iTunes/iTunes Music Library.xml"


def check_file(dict):
location = dict['Location']

try:

# try the url
u = urllib.urlopen(location)

# close it
u.close()

# if we get here, it was ok
ok = True

except:
ok = False

if (not ok):
print "file not found: " + location


print "Reading iTunes xml file..."
x = plistlib.readPlist(filename)

tracks = x['Tracks']

for key in tracks.iterkeys():
check_file(tracks[key])

No comments: