如何解决eyed3库报错AttributeError: 'NoneType' object has no attribute 'tag'
问题分析与解决
从错误日志能明确看到,eyed3.core.load(new_file)返回了None,这说明eyed3没能成功加载目标MP3文件,后续直接访问mySong.tag自然会抛出AttributeError。
解决步骤
先校验加载结果
在访问mySong.tag之前,必须先判断mySong是否为None,避免空对象操作:if new_file.exists(): print(f'{data[i][0]} has been\033[1;32;40m successfully downloaded.\033[m ') mySong = eyed3.core.load(new_file) print(new_file) print(mySong) # 先判断mySong是否加载成功 if mySong is None: print(f"Failed to load {new_file}, skip tagging") continue if mySong.tag is None: mySong.initTag() mySong.tag.images.set(type_=3, img_data=None, mime_type=None, description=None, img_url=data[i][3]) mySong.tag.save()排查加载失败根源
- 检查MP3文件完整性:用系统播放器或其他音乐工具打开文件,确认文件没有损坏
- 核对文件路径与名称:打印
new_file的绝对路径,确认和实际文件路径一致;注意错误日志中原文件名是I Ain't Worried.mp3,但代码输出的是I Aint Worried.mp3,可能存在下载时的字符丢失问题 - 更新eyed3版本:旧版本对部分MP3编码支持有限,执行
pip install --upgrade eyed3更新到最新版 - 检查文件编码:若文件采用eyed3不支持的编码,可尝试用ffmpeg等工具重新编码后再处理
内容的提问来源于stack exchange,提问作者Melabr
相关产品推荐
相关产品推荐

