Wednesday, January 06, 2010

Delete iTunes Duplicates using Python.

Update: Hmmm, this seems to screw up the iTunes library. Don't run this!.

A rather quick and dirty script I wrote to delete duplicate songs in my iTunes folder. If you run it from your home directory, it creates a shell script which will delete the duplicates.

Make sure you inspect the shell script before you run it!

import os

filenames = dict()
for root, dirs, files in os.walk("Music/"):
for fn in files:
filenames[fn] = os.path.join(root, fn)

print len(filenames), "files."

names = set(filenames.keys())
dupes = set()

for fn in filenames.keys():
if fn[-5] in "123456789" and fn[-6] == " ":
on = fn[:-6] + fn[-4:]
if on in names: dupes.add(filenames[fn])

print len(dupes), "duplicates."

open("delete_dupes.sh","w").write("\n".join('rm "%s"'%f for f in dupes))

3 comments:

Virgil Dupras said...

Deleting duplicate songs in iTunes is a big part of my business. By "Screwing your iTunes Library", do you mean creating dead references? If yes, you can fix this, with Python and appscript. See the source on my bitbucket repo

Chris Adams said...

Here's a script I've been using for a few years:

http://github.com/acdha/unix_tools/blob/master/bin/dupinator.py

It compares files by hash so it'll catch files which have the same contents but different names and ignore duplicate filenames with different content (I have a bunch of these) and has options to move files to the trash instead of simply deleting them.

I've typically used that in combination with Super Remove Dead Tracks but Virgil's appscript approach looks interesting, too.

Anonymous said...

Or you can try exporting your duplicates and upload it to mDecks.com

http://mdecks.com/mdeldupform.php

Popular Posts