Skip to content

Instantly share code, notes, and snippets.

@cherian
Created July 20, 2010 12:36
Show Gist options
  • Save cherian/482899 to your computer and use it in GitHub Desktop.
Save cherian/482899 to your computer and use it in GitHub Desktop.
import sys
import os , shutil
rootdir = None
try:
rootdir = sys.argv[1]
except IndexError:
print 'Directory not specified. Specify it in the format ' + sys.argv[0] + ' c:\songs'
sys.exit()
if not os.path.isdir(rootdir):
print 'argument is not not a directory'
sys.exit()
dirDict = {}
for root,dirs,files in os.walk(rootdir):
for file in files:
print file
rootfilepath = os.path.join(rootdir, file)
album = file.partition('-')[0].strip()
if not dirDict.has_key(album):
dirDict[album] = [rootfilepath]
else:
albumfiles = dirDict.get(album)
albumfiles.append(rootfilepath)
for k,v in dirDict.iteritems():
albumDir = os.path.join(rootdir, k)
if not os.path.exists(albumDir):
os.mkdir(albumDir)
for file in v:
print 'album dir is ' + albumDir
print 'copying ' + file + ' to directory ' + albumDir
try:
shutil.move(file.strip(),albumDir)
except IOError:
print 'Could not copy ' + file + 'to ' + albumDir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment