Re: (noob alert) why doesn't this work?
On Tue, 22 Mar 2005 12:10:50 +0100, Bouke Woudstra [EMAIL PROTECTED] wrote: 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 Perhaps you get to line 56 without having gone through line 51? Might one of your flac files not have a title line? 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? Nope. Looks asynchronous to me. -- Cheers, Simon B, [EMAIL PROTECTED], http://www.brunningonline.net/simon/blog/ -- http://mail.python.org/mailman/listinfo/python-list
Re: (noob alert) why doesn't this work?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: (noob alert) why doesn't this work?
Thanks for all suggestions. Your first point I knew, but was too lazy to type it. It made no difference for the test files had all tags. Knowing that it was not a asynchrous thing helped me a lot though. There had to be something wrong with the command line for metaflac. It turned out that some flac files have tags like Artist=artistname and others have artist=artistname. Therefore it couldn't find the artist! So now I just look for 'rtist=' which works great. Thanks for all the help! Op dinsdag 22 maart 2005 12:18, schreef Simon Brunning: On Tue, 22 Mar 2005 12:10:50 +0100, Bouke Woudstra [EMAIL PROTECTED] wrote: 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 Perhaps you get to line 56 without having gone through line 51? Might one of your flac files not have a title line? 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? Nope. Looks asynchronous to me. -- Cheers, Simon B, [EMAIL PROTECTED], http://www.brunningonline.net/simon/blog/ -- http://mail.python.org/mailman/listinfo/python-list
Re: (noob alert) why doesn't this work?
Bouke Woudstra wrote: It turned out that some flac files have tags like Artist=artistname and others have artist=artistname. Therefore it couldn't find the artist! So now I just look for 'rtist=' which works great. You might want try using something like this: wanted = set('artist album date title tracknumber genre'.split()) ... def read_and_call(self, flac, source): parts = {} for line in source: try: head, remainder = line.split('=', 1) except ValueError: pass # No equal sign in the line else: head = head.strip().lower() if head in wanted: parts[head] = remainder.strip() self.wav2mp3(flac, **parts) -- http://mail.python.org/mailman/listinfo/python-list
Re: (noob alert) why doesn't this work?
On Tue, 22 Mar 2005 09:21:49 -0800, Scott David Daniels [EMAIL PROTECTED] wrote: Bouke Woudstra wrote: It turned out that some flac files have tags like Artist=artistname and others have artist=artistname. Therefore it couldn't find the artist! So now I just look for 'rtist=' which works great. You might want try using something like this: wanted = set('artist album date title tracknumber genre'.split()) ... def read_and_call(self, flac, source): parts = {} for line in source: try: head, remainder = line.split('=', 1) except ValueError: pass # No equal sign in the line else: head = head.strip().lower() if head in wanted: parts[head] = remainder.strip() self.wav2mp3(flac, **parts) Doesn't wav2mp3 have to have default arg values for that, if parts doesn't have all the args? Alternatively (and untested!) hopefully providing all wav2mp3 args in order: def flactags(self): flacfiles = glob.glob('*flac') flacfiles.sort() for flac in flacfiles: cmd = 'metaflac --export-tags=- %s' % flac # collect available tags, then make arg list with '??' for missing values tagdict = dict((pair[0].strip().lower(), pair[1].strip()) for pair in (line.split('=', 1) for line in os.popen(cmd).readlines()) if len(pair)==2) args = [tagdict.get(tag, '??') for tag in 'artist, album, year, number, genre'.split()] self.wav2mp3(flac, *args) Regards, Bengt Richter -- http://mail.python.org/mailman/listinfo/python-list
Re: (noob alert) why doesn't this work?
On Tue, 22 Mar 2005 12:10:50 +0100, Bouke Woudstra [EMAIL PROTECTED] wrote: 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) I'll point out, there's a bug here. If any flac file is missing any one of these tags, but a previous one does have the tag, (i.e. 'Genre'), then the previous tag will be used, because you don't reset the variables each time around the loop. -- Stephen Thorne Development Engineer, NetBoxBlue.com -- http://mail.python.org/mailman/listinfo/python-list