3

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) ~\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
jedwards
  • 29,432
  • 3
  • 65
  • 92
ch1nmay
  • 81
  • 1
  • 7
  • 1
    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 '18 at 00:54
  • 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 '18 at 01:12
  • 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 '18 at 01:20

0 Answers0