Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类型错误:';int';对象没有属性'__获取项目';(该值为0)_Python_List - Fatal编程技术网

Python 类型错误:';int';对象没有属性'__获取项目';(该值为0)

Python 类型错误:';int';对象没有属性'__获取项目';(该值为0),python,list,Python,List,我不明白为什么我会犯那样的错误。。以下是我的职能: 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 = {}

我不明白为什么我会犯那样的错误。。以下是我的职能:

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
这首歌在歌曲目录()中的歌曲标题中调用。

在我的日志中,我总是有那个错误。我做错了什么? 在它像一个符咒一样工作后,第一次返回0

编辑代码:

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)
但现在,它说轨迹在被引用之前就被使用了(和以前一样的问题)。我应该用像这样的东西吗

if track is not None

??虽然…

但在某些情况下,您的函数
磁道读取器
返回
0
。因此
tmp_数据
可以在执行后
0

tmp_data = track_reader(tmp, extension(tmp))
所以你会得到一个例外

songs[tmp_data['path']] = tmp_data['title'] 
总而言之:如果您选择的类型不是
mp3
mpeg3
mp4

要解决此问题,您可以执行以下操作:

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)

测试track_reader方法的返回值,而不是0 return
None
,并执行如下操作:

try:
            tmp_data = track_reader(tmp, extension(tmp))
            if tmp_data is not None:
                        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)

您在第一个函数中索引了
tmp_数据['path']
,如果出现错误,该函数为0。此外,关于
track\u reader
@krish的返回类型,您的文档是错误的。关于编辑的代码,您需要在函数开头将track变量初始化为None。您还必须检查tmp_数据,正如我在解决方案中向您展示的那样。
try:
            tmp_data = track_reader(tmp, extension(tmp))
            if tmp_data is not None:
                        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)