473,657 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(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: UnboundLocalErr or: 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('*fla c')
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('*fla c')
flacfiles.sort( )
for flac in flacfiles:
cmd = 'metaflac --export-tags=- "%s"' % flac
for line in os.popen(cmd).r eadlines():
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(fl ac, title, artist, album, year, number, genre)
x = program()
x.makewavdir()
x.flac2wav()
x.makemp3dir()
x.flactags()
Jul 18 '05 #1
1 1851
>
The error thrown is: UnboundLocalErr or: local variable 'title' referenced
before assignment


That should be pretty obvious: The UnboundLocalErr or 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 UnboundLocalErr or

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
16857
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 in certain situations. <% // Check for expired session log.debug("session ID = " + session.getAttribute("session_loginID")); if (session.getAttribute("session_loginID") == null)
7
5740
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: http://groups.google.com/groups?hl=en&lr=&threadm=F2QCa.15007%24Xl.400929%40twister.rdc-kc.rr.com&rnum=2&prev=/groups%3Fq%3Djapanese%2Balert%26hl%3Den%26lr%3D%26group%3Dcomp.lang.javascript%26selm%3DF2QCa.15007%2524Xl.400929%2540twister.rdc-kc.rr.com%26rnum%3D2 This post implyes that it's possible to use the \uXXXX...
4
1593
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" nowrap="nowrap" background="/work/phc1.10/images/layout/main_nav_bg.gif"> <a href="http://localhost/work/phc1.10/index.php?page=homecare" onmouseover="imgSwap('','gnav_hc_on','gnav_hc_on')" onmouseout="imgSwap('','gnav_hc_off','gnav_hc_off')">
2
1716
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 can be made. When the user presses on the OK button, I need the changed information in my main form, so the code can act upon the (possible) changes made. So what I do is: I instantiate the form, I show the form, data gets changed, but how do I...
42
3429
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: ---------------------------------------------------- TypeError: addFile() takes exactly 1 argument (2 given) ----------------------------------------------------
1
2050
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 the alert call in place below, but a delay (using another function defined elsewhere, which does work by the way) doesn't. Similarly if no delay is put in place it fails to complete the next line. It certainly appears that a value hasn't been finalised...
1
1966
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 and Parameter Editor setup when I created my SQLadapter, along with Select and Delete. Here is the Update Command: UPDATE TBLUPSData SET UPSModel = @original_UPSModel, UPSLocation = @original_UPSLocation, UPSDeviceAttached =...
4
4607
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 on the web page you are looking at and then displays the results as a table. There is an 'IF' statement which only writes the table if there are filtered records to display. Now for the bit that is driving me crazy . . . The function only works if...
0
1378
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 program I have a picture box that when the user clicks it I want it to change the picture, and when the user clicks it again I want it to change back to the first image. So I figured a simple if statement would work, well it doesn't. Again I'm a...
0
8316
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8833
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.