我想为以下函数创建 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