I have seen the hard drives of many friends become polluted with replicas of tracks in iTunes. This is not a very common occurrence on my machine and I am not entirely sure why it happens. It may have something to do with Senuti or maybe they are deleting albums and re-importing them, but the albums never actually get removed from the file-system. Either way, I wrote this script on a lazy afternoon after I handed my friend a CD to import into his library and his disk was full. After physically clicking through individual files and deleting them, I figured it would be faster, and definitely more educational, to write the python.
link: http://www.datasingularity.com/code/python/iTunes_cleaner.tar.gz
This isn’t exactly full-proof so let me explain how it works before you just start deleting songs from your library. This code relies on a naming convention that iTunes uses when duplicating files in an album. For example, the track “All your base are belong to them” is duplicated as “All your base are belong to them 1″, “All your base are belong to them 2″, and etc. So this script goes through every album folder and looks for track titles that exist as is and the duplicate that has the numbering convention. I obviously can’t explain this in words too well. It uses the glob module (a python module for *nix type filename parsing). Here is the heart of the logic:
for track in tracks:
trackFull = albumPath+'/'+track
if trackFull not in copyPaths:
try:
tCopies = glob.glob(os.path.splitext(trackFull)[0]+
' [0-9]'+os.path.splitext(trackFull)[1])
copyPaths.extend(tCopies)
except:
if albumPath not in invalid:
invalid.append(albumPath)
Now how to use this. I am, for safety’s sake, going to assume that you may not have experience with python or using a terminal. BTW, I don’t know if this works on Windows and you probably need python 2.5 or above:
- You need python installed, for both OS X and Windows I suggest going to download IDLE (an IDE that comes with python).
- Open this script with IDLE and run it from the menu bar [ Run >> Run Module ]
- It will first ask you for the full path to your iTunes directory [ e.g. mine is /Users/ben/Music/iTunes ]
- It will create a queue of files to delete and ask if you want to see them.
- Hit ‘y’ and [Enter] for yes.
- Review the titles and make sure that they are all what you are looking for.
- It will ask if you want to delete individually or all. Answer no (‘n’) to delete all of them.
- Answer yes (‘y’) to delete individually like the *nix command [ rm -ir * ]
- The rest is self-explanatory.
It may ask if you want to see the skipped albums. This is because it skips the album if there are some problems with the string pattern matching modules [ I'm not going into detail ]. Just go through those albums manually.
0 comments ↓
There are no comments yet...Kick things off by filling out the form below.
Leave a Comment