473,385 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

(noob alert) why doesn't this work?

Hi,

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"
else:
os.mkdir('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
print

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"
else:
os.mkdir('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')
flacfiles.sort()
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()
x.makewavdir()
x.flac2wav()
x.makemp3dir()
x.flactags()
Jul 18 '05 #1
1 1839
>
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
file.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: schieco | last post by:
The following code always prints the debug lines inside the conditional if statement before and after the alert statement when the session has timed out, but the alert and redirect only appear/work...
7
by: Daniel | last post by:
I`m trying to show japanese characters in an alert, like; alert('実装されていませ'); This doesn't work as shown in this post:...
4
by: Matt | last post by:
i know more-or-less zero about javascript, but my client wants a rollover and he created this code, which doesn't work. All comments are appreciated. 1. the tag: <td align="center" width="66"...
2
by: rs.franken | last post by:
Hi all, I am writing a little application. The main form presents a main menu and within this menu there is an "options" part. This option will show a new form, where some application settings...
42
by: Holger | last post by:
Hi guys Tried searching for a solution to this, but the error message is so generic, that I could not get any meaningfull results. Anyways - errormessage:...
1
by: dsloan | last post by:
This problem is very similar to that experienced in http://www.thescripts.com/forum/thread147783.html, but the solution there doesn't seem to apply here. I have a function which works fine with...
1
by: Japskunk | last post by:
I am having trouble updating a SQL table through the GridView "Auto" Enable Edit Feature... I am connecting to a SQL 2000 Server with a SQLDataSource I have created the Update Query in the Command...
4
by: clentoc | last post by:
I'm pretty new to javascript so this may well be a really basic error, but I've spent hours trying to fix it with no joy. I have written some code which looks in a CSV file, filters it depending...
0
by: irtehnoob | last post by:
Hi there, I'm pretty new to c# (started up a few days ago) and I'm making a simple program, so far everything is going good except I can't figure somethine out.. hence why I'm here. Anyways, in my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.