python - Mutagen Python FileNotFoundError 处理
问题描述
我想为以下函数创建 FileNotFoundError 错误,其中我使用诱变剂包的 EasyID3 类获取 mp3 文件的类型,但我仍然收到 FileNotFoundError。我正在使用 Python 3.6.5 的 Anaconda 环境
from mutagen.easyid3 import EasyID3
def getGenre(filename):
try:
audiofile = EasyID3(filename)
#No genre
if not audiofile['genre']:
return None
else:
return audiofile['genre']
except (FileNotFoundError,IOError):
print("Wrong file or file path")
getGenre("xyz.mp3")
输出:
FileNotFoundError Traceback (most recent call last) ~\Anaconda2\envs\torchenv\lib\site-packages\mutagen\_util.py in
_openfile(instance, filething, filename, fileobj, writable, create)
234 try:
--> 235 fileobj = open(filename, "rb+" if writable else "rb")
236 except IOError as e:
FileNotFoundError: [Errno 2] No such file or directory: 'xyz.mp3'
During handling of the above exception, another exception occurred:
MutagenError Traceback (most recent call last) <ipython-input-50-08d31a5afa8e> in <module>()
----> 1 getGenre("xyz.mp3")
<ipython-input-49-c80f3c838450> in getGenre(filename)
2 def getGenre(filename):
3 try:
----> 4 audiofile = EasyID3(filename)
5 #No genre
6 if not audiofile['genre']:
~\Anaconda2\envs\torchenv\lib\site-packages\mutagen\easyid3.py in
__init__(self, filename)
168 self.__id3 = ID3()
169 if filename is not None:
--> 170 self.load(filename)
171
172 load = property(lambda s: s.__id3.load,
~\Anaconda2\envs\torchenv\lib\site-packages\mutagen\_util.py in wrapper(*args, **kwargs)
167 def wrapper(*args, **kwargs):
168 try:
--> 169 return func(*args, **kwargs)
170 except exc_dest:
171 raise
解决方案
推荐阅读
- angular - 角度 - 如何检测浏览器在更改后完成重新渲染
- python - 类型错误:将神经网络添加到堆叠集成时无法腌制 _thread.RLock 对象
- mysql - 如何获取此查询中重复项的大小?
- uwp - 从 AppxBundle 安装时不显示迁移到 UWP 的 Windows 窗体应用
- angular - 隐藏在某些特定页面中的 Ionic 5 离子标签
- android - 在 android 10 中启动时设置 Activity 崩溃在 android 28<. 如何解决?
- mysql - MYSQL 自动递增日期日期年份
- swift - 以编程方式填充 Swift NSToolBar 时出现问题,项目不可见?
- azure-devops - Azure DevOps 2019 - 仪表板图表不向下钻取
- performance - 正则表达式测试 200 到 299 之间的数字 - 什么更快