Mutagen Python FileNotFoundError handling


Mutagen Python FileNotFoundError handling



I want to create FileNotFoundError error for the below function where i get genre of a mp3 file using EasyID3 class of mutagen package, But i am still getting FileNotFoundError. i am using Anaconda environment with Python 3.6.5


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")



Output:


FileNotFoundError Traceback (most recent call last) ~Anaconda2envstorchenvlibsite-packagesmutagen_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']:

~Anaconda2envstorchenvlibsite-packagesmutageneasyid3.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,

~Anaconda2envstorchenvlibsite-packagesmutagen_util.py in wrapper(*args, **kwargs)
167 def wrapper(*args, **kwargs):
168 try:
--> 169 return func(*args, **kwargs)
170 except exc_dest:
171 raise





It looks like mutagen wraps the FileNotFoundError in it's own error: MutagenError (actually, it looks like it mishandles it) -- in any case try adding that to your catch statement (you may need to import that name too).
– jedwards
Jun 30 at 0:54



FileNotFoundError


MutagenError





When i try to add it to the as - except (FileNotFoundError,IOError, MutagenError): print("Wrong file or file path"), it again shows me the error, but this time -NameError: name 'MutagenError' is not defined
– ch1nmay
Jun 30 at 1:12



except (FileNotFoundError,IOError, MutagenError): print("Wrong file or file path")


NameError: name 'MutagenError' is not defined





That's what I was saying about importing that name, try adding from mutagen import MutagenError at the top with your imports.
– jedwards
Jun 30 at 1:20


from mutagen import MutagenError









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV