Remove Duplicate Files in iTunes

Apple, in its inifinite idiocy, decided that iTunes does not need an easy way to remove duplicate songs en masse. I recently copied my MP3 libraries from three different sources into one place, so there were a lot of duplicates. Rather than take off work for the next two months to remove the duplicates one at a time through iTunes, I whipped up a quick Python script to remove the files.

The duplicates had a single space followed by 1 or two at the end of the file name. For instance:
Blondie – Heart of Glass.mp3
Blondie – Heart of Glass 1.mp3
Blondie – Heart of Glass 2.mp3

My simple script uses this situation to recurively decend through multiple layers of folders and delete the extra files. Here it is:

import os

path = "/Volumes/Untitled/Music"

for dirpath, dirnames, filenames in os.walk(path):
for file in filenames:
f = str(file).strip()
if f.endswith(' 1.mp3') or f.endswith(' 2.mp3'):
fullpath = os.path.join(dirpath, file)
print 'Deleting ' + fullpath
os.remove(fullpath)
This entry was posted in grpug, python, tech. Bookmark the permalink.