Skip to content

Instantly share code, notes, and snippets.

@cherian
Created July 20, 2010 12:36
Show Gist options
  • Select an option

  • Save cherian/482899 to your computer and use it in GitHub Desktop.

Select an option

Save cherian/482899 to your computer and use it in GitHub Desktop.

Revisions

  1. cherian created this gist Jul 20, 2010.
    35 changes: 35 additions & 0 deletions mp3folders.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    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