0

i wrote python code to organize 40G's of music, but it only works on my computer, so i wanted to generalize the code so it works in what ever directory in what ever computer.

import os #imports os functions
import eyed3 #imports eyed3 functions
import errno
import shutil

root_folder = os.getcwd()

files = os.listdir(root_folder) #lists all files in specified directory


for file_name in files:
    if file_name.endswith('.mp3'): #if file ends with ".mp3" it continues onto the next line

        abs_location = '%s/%s' % (root_folder, file_name)

        try:
            song_info = eyed3.load(abs_location) #loads each file into eyed3 and assignes the return value to song_info
        except IOError:
            pass
        if song_info.tag is None:
            print 'Skipping %s' % abs_location
            continue
        if song_info is None:
            print 'Skipping %s' % abs_location
            continue
            print 'Skipping %s' % abs_location
            continue
        try:
            os.mkdir(os.path.expanduser('~/Desktop/mp3-organizer/%s' % song_info.tag.artist))
        except OSError as e:
            if e.errno!= errno.EEXIST:
                raise
        except UnicodeDecodeError:
            pass
        try:
            os.mkdir(os.path.expanduser('~/Desktop/mp3-organizer/%s/%s' % (song_info.tag.artist, song_info.tag.album)))
        except OSError as e:
            if e.errno!= errno.EEXIST:
                raise
        except OSError:
                continue
        print song_info
        print song_info.tag.artist
        try :
            shutil.move('%s' % file_name, '%s/%s' % (song_info.tag.artist, song_info.tag.album))
        except UnicodeDecodeError:
            pass
        except shutil.Error:
            try:
                os.renames('%s' % file_name, '%s_%s' % (file_name, song_info.tag.artist))
            except OSError:
                pass
    else:
        pass

#improvements
    #have this work on other computers

i want to change the lines where "~/Desktop/mp3-organizer/" is used into 'root_folder/%s' % song_info.tag.artist, where "root_folder" = present working directory

but i know that doesn't work, i just don't know how to "word" it.

to recap, the goal is to have the code be working in the directory the script is placed in. not only work with the specific directories in my computer.

any help is apreciated

ntoscano
  • 67
  • 5
  • Tip: use `os.path.join`. Side note: this is how you should _not_ use comments in the code. Also: why do you `except OSError` two times in a row? – Lev Levitsky Jan 11 '13 at 23:03

2 Answers2

1

You can get the current directory like this:

>>> import os
>>> os.curdir
'.'
>>> d = os.path.realpath(os.curdir)
>>> print d
/Users/jdoe

Then, to add the subdirectory:

>>> print os.path.join(d, 'mp3-organizer')
/Users/jdoe/mp3-organizer
bogatron
  • 18,639
  • 6
  • 53
  • 47
  • i'm trying `os.path.join(root_folder, '%s' % song_info.tag.artist)`, with `root_folder = os.path.realpath(os.curdir)` but it's not making any directory – ntoscano Jan 11 '13 at 23:22
  • That statement doesn't create a directory - it creates a properly formatted name (path) for the directory. To create the directory, pass the result of that statement to `os.mkdir` or `os.makedirs` (if you also need to create the higher-level dirs). – bogatron Jan 12 '13 at 19:56
0

To get the directory of currently script, try:

import os
root_folder = os.path.abspath(__file__) 
mp3_dir = os.path.join(root_dir, 'mp3-organizer')

now you can populate the mp3_dir no matter what current directory under which your run the script

Wu Li
  • 789
  • 5
  • 6
  • The variable `root_dir` is undefined, assume you meant `root_folder`. However the directory the currently running script is in is not necessarily going to be the same as the OS's current working directory, which is what the OP said they wanted. – martineau Jan 12 '13 at 03:15