Created
July 20, 2010 12:36
-
-
Save cherian/482899 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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