I am coding a little script that gets metadata from a sound file and creates a string with the required values. I know that I am doing something wrong, but I'm not sure why, but this is probably the way I repeat if's. When I run the code:
import os, mutagen XPATH= "/home/xavier/Code/autotube/tree/def" DPATH="/home/xavier/Code/autotube/tree/down" def get_meta(): for dirpath, directories,files in os.walk(XPATH): for sound_file in files : if sound_file.endswith('.flac'): from mutagen.flac import FLAC metadata = mutagen.flac.Open(os.path.join(dirpath,sound_file)) for (key, value) in metadata.items(): #print (key,value) if key.startswith('date'): date = value print(date[0]) if key.startswith('artist'): artist = value #print(artist[0]) if key.startswith('album'): album = value #print(album[0]) if key.startswith('title'): title = value #print(title[0]) build_name(artist,album,title) # UnboundLocalError gets raised here def build_name(artist,album,title): print(artist[0],album[0],title[0])
I get the desired result or error, randomly:
RESULT:
1967 Ravi Shankar & Yehudi Menuhin West Meets East Raga: Puriya Kalyan
ERROR:
Traceback (most recent call last): File "<stdin>", line 39, in <module> File "<stdin>", line 31, in get_meta build_name(artist,album,title) UnboundLocalError: local variable 'album' referenced before assignment
source share