#!/usr/bin/env python import os from mutagen.easyid3 import EasyID3 cwd = os.getcwd() files = os.listdir(cwd) Failures=0 Success=0 for file in files: if file.endswith(".mp3"): try: #print "Processing: ", file audio=EasyID3(file) album=audio['album'][0] artist=audio['artist'][0] if len(album) > 0 and len(artist) > 0: clean_artist = artist.replace("'","") clean_artist = clean_artist.replace( " ", "_" ) artist_dir = os.path.join( cwd, clean_artist ) if not os.path.exists( artist_dir ): os.mkdir(artist_dir) clean_album = album.replace("'","") clean_album = clean_album.replace( " ", "_" ) album_dir = os.path.join( artist_dir, clean_album ) if not os.path.exists( album_dir ): os.mkdir( album_dir ) destination_file = file.replace( "'", "" ) destination_file = destination_file.replace( " ", "_" ) destination = os.path.join( album_dir, destination_file ) #print 'mv "%s" "%s"' % (file, destination) os.system( 'mv "%s" "%s"' % (file, destination) ) Success+=1 except Exception,e: print "Failed: ", str(e) print "album_dir:", album_dir print "destination_file:", destination_file print Failures+=1 print "Success:", Success print "Failures:", Failures