hey every1, im a noob pythoneer.. i wanted to change the names of all music files using their metadata obtained from mminfo utility. I came up with the following hideous looking code..

import os

os.chdir(raw_input('plz enter directory name: '))
lst=os.listdir(os.getcwd())
class process():
	def getmetadata(self,filename):
		command='mminfo %s|grep artist>temp.txt'%filename;#print command
		os.system(command)
		q=file('temp.txt');self.artist=self.get(q.read());q.close()
		command='mminfo %s|grep title>temp.txt'%filename
		os.system(command)
		q=file('temp.txt');self.title=self.get(q.read());q.close()

	def __init__(self,filename):
		self.getmetadata(filename)
		os.rename(filename,self.artist+'_'+self.title+'.mp3')

	def get(self,s):#this returns the string after ':' in s
		num=s.find(':')
		return s[num+1:]
#print lst
for i in lst:
	#print i
	instance=process(str(i))
#os.remove('temp.txt')

yea.. u guessed right.. it doesn't work.. i checked the temp.txt.. it's not retrieving the proper data using the 'command'.. its always a blank file..

i think there has to be a better way to code the getmetadata() func. The reason im using the intermeditate temp.txt file is because i cant just write

t=os.system('mminfo file.mp3')

this just assigns either 0 or 1 to t and not the actual output from the command.
Plz tell me what is rong with this code.. also if you can tell me an alternate way to code getmetadata() plz do.. it seems ive probably coded the least efficient func of all time.. it takes a good 6 seconds to work on a dir with just 10 files!!:(

Try to isolate the issue: Whenever I mesh python and the command line I usually try to test out the command first just using a command window. Then you can be sure that mminfo is working correctly. If so, maybe it's os.system call. Could be an enviroment issue, try putting the entire path to the mminfo executable, instead of just mminfo.

Additionally, you may want to make your temp.txt file part of the class. self.metadata_file or something similar. Also, when you are writing classes, capitals are nice and you don' t need the (). Ex:

class Process:
  def __init__(self):
          pass

Agreed on the hideous part. Split those lines up instead of the semicolons. I had to test that, I've never seen that before. :)

hmm ok.. sry im not well versed with the normal way of writing python progs yet.. gud news is.. this prog does what its sups o but only if there is one file in the directory.. hav luk guys.. plz tel me y its not working for more than one.. i hav a feeling it mayb because of the scope of objects..

You gonna have to give a little more info. You have a print statement commented out, does it at least try and process all the files? Is there an error?

And yes, you can't reference temp.txt in a class FIRST, and then try to reference it in the body of your script. Try passing it to the class.

Help me help you. ;)

This article has been dead for over six months. Start a new discussion instead.