Python script to find Amazon store MP3 files

I’m working on rationalizing my MP3 collection.  Here’s a Python script to print out the names of all songs that seem to have been bought from Amazon MP3. It uses the presence of “Amazon” in the ID3v2 comment tag. It’s not entirely reliable; songs I bought in 2007, 2008 are missing that tag.


#!/usr/bin/python

"""Print out names of MP3 files that are from Amazon.
Works by looking for the Amazon ID3v2 comment tag with a song ID.
This method is not complete: some early Amazon sales don't have this."""

import sys, os
from mutagen.id3 import ID3

def isAmazon(fn):
  "Return the Amazon comment if it's an Amazon MP3, else None"
  id3=ID3(fn)
  for c in id3.getall('COMM'):
    if "Amazon" in str(c):
      return str(c)
  return None

for dirpath, dirnames, files in os.walk('.'):
  for name in files:
    if name.endswith(".mp3"):
      fn = os.path.join(dirpath, name)
      try:
        if isAmazon(fn):
          print fn
      except:
        pass # ignore errors

sys.exit(0)