By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,942 Members | 1,788 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,942 IT Pros & Developers. It's quick & easy.

(noob alert) why doesn't this work?

P: n/a

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()
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a
The error thrown is: UnboundLocalError: local variable 'title' referenced
before assignment

That should be pretty obvious: The UnboundLocalError comes up when you try
to access a variable that hasn't been assigned a value before. E.g try this
in an interactive python session:

foo = "hello"
print foo
print bar # This will raise UnboundLocalError

Now in your code, you have a conditional setting of diverse variables. So
only if

if 'Title' in line:
***title = line[6:-1]

executes, a title is there. Later, you _always_ use title. So you have to do
it like this:

title = "Unknown" # or empty or whatever
if 'Title' in line:
***title = line[6:-1]

Then title will always be there.

The reason that it works for _one_ but not for all is simply that by chance
the one file _had_ a title, but at least one of all the files hadn't. So it
crashes. If you'd only try that file, it would also crash with only one

Diez B. Roggisch
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.