TypeError: object 'int' does not have attribute '__getitem__' (value is 0)

advertisements

I can't understand why I get that error.. these are my functions:

def song_titles_in_dir(dir_path):
    """
    :rtype: dict
    :param dir_path: directory to scan
    """
    list_dir = absolute_file_paths(dir_path)  # get absolute path of songs in the /artist/album folder
    songs = {}

    for tmp in list_dir:
        try:
            tmp_data = track_reader(tmp, extension(tmp))
            songs[tmp_data['path']] = tmp_data['title']  # appending all the titles in one list to check for them later
        except TypeError as err:
            logger(log_path, "TypeError: %s" % err)
    return songs

This one is called in song_titles_in_dir()

def track_reader(file_path, type):  # returns list with title, artist, album

    """
    :param file_path: the audio file that has to be categorized
    :param type: which type the audio file is [mp3, mp4..]
    :rtype : dict
    """

    if type in ['.mp3', '.mpeg3']:
        track = EasyID3(file_path)
    elif type == '.mp4':
        track = EasyMP4(file_path)
    else:
        return 0

    try:
        # track_has is a list which contains the attributes the song has
        track_has = []
        for x in ('title', 'artist', 'album'):
            if track[x][0]:
                track_has.append(x)
        track_data = {'path': file_path}
        for prop in track_has:
            track_data[prop] = track[prop][0].encode('ascii', 'ignore')  # it was encoded in unicode
        return track_data
    except Exception as err:
        logger(log_path, err)
        return 0

In my logs I always have that error. What am I doing wrong? It returns 0 the FIRST time, after it works like a charm...

EDITED CODE:

def track_reader(file_path, type):  # returns list with title, artist, album

    """
    :param file_path: the audio file that has to be categorized
    :param type: which type the audio file is [mp3, mp4..]
    :rtype : dict
    """

    if type in ['.mp3', '.mpeg3']:
        track = EasyID3(file_path)
    elif type == '.mp4':
        track = EasyMP4(file_path)

    if track:
        try:
            # track_has is a list which contains the attributes the song has
            track_has = []
            for x in ('title', 'artist', 'album'):
                if track[x][0]:
                    track_has.append(x)
            track_data = {'path': file_path}
            for prop in track_has:
                track_data[prop] = track[prop][0].encode('ascii', 'ignore')  # it was encoded in unicode
            return track_data
        except Exception as err:
            logger(log_path, "Exception: %s" % err)

But now, it says that track is used before it is referenced (same problem as before). Should I use something like

if track is not None

? It does NOT work though...


Your function track_reader is returning 0 in some cases. So tmp_data can be 0 after executing:

tmp_data = track_reader(tmp, extension(tmp))

So you will get that exception in the line

songs[tmp_data['path']] = tmp_data['title']

To sum up: you will get that exception if your selected type is not mp3 mpeg3 or mp4

For solving it you can do something like:

for tmp in list_dir:
    try:
        tmp_data = track_reader(tmp, extension(tmp))
        # Check that tmp_data is not falsy or not contains 'path'
        if tmp_data and 'path' in tmp_data:
            songs[tmp_data['path']] = tmp_data['title']  # appending all the titles in one list to check for them later
    except TypeError as err:
        logger(log_path, "TypeError: %s" % err)