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