Skip to content

Instantly share code, notes, and snippets.

@Qalthos
Created February 21, 2014 04:27
Show Gist options
  • Save Qalthos/9128758 to your computer and use it in GitHub Desktop.
Save Qalthos/9128758 to your computer and use it in GitHub Desktop.
re-organize music
#!/usr/bin/env python
from __future__ import unicode_literals, print_function
import os
import re
import mutagen
for path, dirs, files in os.walk('old'):
for file_ in files:
src = os.path.join(path, file_)
ext = file_.rsplit('.', 1)[1]
try:
tag = mutagen.File(src, easy=True)
genre = (tag.get('genre') or ['uncategorized'])[0]
# not everyone has album artist set.
artist = (tag.get('albumartist') or tag.get('performer') or tag['artist'])[0]
album = tag['album'][0]
num = int(tag['tracknumber'][0].split('/')[0])
song = re.sub('/', '|', tag['title'][0])
dest_dir = os.path.join(genre, artist, album)
dest_file = '{0:02d} - {1}.{2}'.format(num, song, ext)
except Exception as e:
print(e)
dest_dir = 'error'
dest_file = file_
dest = os.path.join(dest_dir, dest_file)
os.renames(src, dest.encode('utf8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment