[英]TypeError: 'int' object has no attribute '__getitem__' (the value is 0)
我不明白为什么会收到该错误。这些是我的功能:
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
这个在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
在我的日志中,我总是有该错误。 我究竟做错了什么? 像超级按钮一样工作后,第一次返回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
? 它虽然不起作用...
在某些情况下,您的函数track_reader
返回0
。 因此,执行后tmp_data
可以为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,返回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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.