(noob alert) why doesn't this work?

Bouke Woudstra bouke at gentoolinux.demon.nl
Tue Mar 22 12:10:50 CET 2005


I'm a bit stuck with this python script. It's aim is to encode all flac files 
to wav and then to mp3. The only problem I have is to preserve the tags. The 
code works when there's just one flac file in a directory but fails for more. 
I can't see why the for loop fails here (flactags). 

The error thrown is: UnboundLocalError: local variable 'title' referenced 
before assignment

I don't understand why this isn't assigned. I suspect that self.wav2mp3 
doesn't wait for the first part to finish, but how to force it to wait?

Can somebody help please? 

The code:

#!/usr/bin/env python
import os
import glob
import sys

class program:
	def makewavdir(self):
		if os.path.isdir('wav'):
			print "There already exists a directory named wav"
			print "The album is probably already decoded to wav"
	def flac2wav(self):
		flacfiles = glob.glob('*flac')
		for flac in flacfiles:
			print '*' * 70
			print 'Starting decoding %s to wav' % flac
			os.system('flac -d -s "%s" -o wav/"%s".wav' % (flac, flac[0:-5]))
		print '*' * 70
		print "All files are decoded to wav in the folder wav"
		print '*' * 70
	def makemp3dir(self):
		if os.path.isdir('mp3'):
			print "There already exists a directory named mp3"
			print "The album is probably already encoded in mp3"
	def wav2mp3(self, flac, title, artist, album, year, number, genre):
		os.system('lame --preset standard -V 2 --tt "%s" --ta "%s" --tl "%s"  --ty \ 
		"%s" --tn "%s" --tg "%s" --id3v2-only wav/"%s".wav mp3/"%s".mp3' %(title, \ 								
		artist, album, year, number, genre, flac[:-5], flac[:-5]))
	def flactags(self):
		flacfiles = glob.glob('*flac')
		for flac in flacfiles:
			cmd = 'metaflac --export-tags=- "%s"' % flac
			for line in os.popen(cmd).readlines():
				if 'Artist' in line:
					artist = line[7:-1]
				if 'Album' in line:
					album = line[6:-1]
				if 'Date' in line:
					year = line[5:-1]
				if 'Title' in line:
					title = line[6:-1]
				if 'Tracknumber' in line:
					number = line[12:-1]
				if 'Genre' in line:
					genre = line[6:-1]
			self.wav2mp3(flac, title, artist, album, year, number, genre)

x = program()

More information about the Python-list mailing list