Question : Extracting strings from and renaming OGG files?

I've recently had to convert a load of OMA files to OGG and while the tag data was preserved, the file names are still in hex and grouped into about 6 large folders. I've created a python script to go through and rename them all using information from strings in the ogg files and group them into directories based on the artist info, but I don't know much about Python and am having some difficulty (a load of files with tag data are discarded). Can anyone suggest where I'm going wrong with the below script?

#!/usr/local/bin/python
import re, os, sys, glob

#get all the jpg file names in the current folder
fname = glob.glob("home/me/Music/*.ogg")
#sort the list
fname.sort()

pattern = r'src="([^"]+)\ARTIST="'
result = re.search(pattern, str(fname))

if result:
      Artist=result;
else:
      print "NO artist tag found in "+str(fname)+"\n"

pattern = r'src="([^"]+)\TITLE="'
result = re.search(pattern, str(fname))

if result:
      Title=result;
else:
      print "NO title tag found in "+str(fname)+"\n"
      
pattern = r'src="([^"]+)\ALBUM="'
result = re.search(pattern, str(fname))

if result:
      Album=result;
else:
      print "NO album tag found in "+str(fname)+"\n"


song_info=[Artist, Title, Album];            #get variables and store in list
#Artist has not been defined

if song_info.has_key(Album):                  #Check if album is already in list
      if os.path.isdir(Album):      #Check if a directory has been created for that album
            dir_list=[dir];                  #If so then add to list

      #Otherwise create new one

      if not os.path.isdir(Album):
            os.mkdir(Album)
            os.rename(fname, Album + Artist+"-"+Title+".ogg")      #Move file into it

#If it is not part of an album we know of
try:
      os.rename(fname, Artist+"-"+Title+".ogg")
except:
      print "error: didn't rename"

exit(0);

Answer : Extracting strings from and renaming OGG files?

General suggestion:  Work incrementally:  get some part of the program working and tested, then move on to another bit.  It looks like you are trying to do too much without establishing that anything works.  For example, you might first just try to find the artist in a file.  Then extend that to the title and album.  Then do the renaming or moving, and finally, extend it to multiple files.

Your script is not searching the files.  It is getting a list of file names, turning that list into a string, and searching in that string.  If the strings you want are actually in the files, you will need code to open and read the files.

It looks like the regular expression searches will find the identifying tags, but not the following data.  To get the data, you will need a way to determine where it ends, once the search tells you where it starts.

You don't need the song_info list.

has_key is a function of dictionaries.  It's not defined for lists like song_info.

For variables that may or may not be there, you should arrange it so that the variable is always defined and either has a value (a string, in your case) or is set to None.  Then you can test the variable later to see if it has a value.  With what you have, if an Album, say, is not found, no value is assigned to the name "Album", so you won't be able to do anything with that name.

Renaming a file with a string containing the album name does not move the file into a directory with the same name as the album.  It just renames the file.
Random Solutions  
 
programming4us programming4us