5

I'm using the mutagen module for Python to get the artist of various MP3 files I have.

Here's the code giving the error:

audio = EasyID3(C:\Users\Owner\Music\Music\Blue Öyster Cult\Blue Öyster Cult\Cities on Flame)
print audio["artist"]

The code is working for most of my MP3 files, but there are a select few that continually give the following error:

KeyError: 'TPE1'

And because of that error, I can't see the artist. Note that these MP3 files all have an artist, and none of them have special characters or anything like that.

Why is this happening? And how can I fix it?

Thanks

Cisplatin
  • 2,860
  • 3
  • 36
  • 56
  • It's a `KeyError`. So, something somewhere is accessing a key `"TPE1"` in a container of some sort, probably a dictionary, and it's not there. You posted only the least useful part of the traceback, the error message, so it's impossible for anyone to guess where the problem is. – kindall Jul 21 '13 at 21:46
  • Is this because you aren't using raw string for the path. the module, not able to parse, throwing up a KeyError – P0W Jul 21 '13 at 22:28

3 Answers3

4

Most likely, you're looking for a key which doesn't exist in mutagens id3 dictionary. Do a simple check like you would do for a regular dictionary:

if 'artist' in audio:
  print audio['artist']

I've tried with and without ensuring that the argument is Unicode and it works in both cases with Python 2.7.3

Tiberiu C.
  • 3,365
  • 1
  • 30
  • 38
1

This is probably because you removed its value manually via the file properties/details.
That's what happened with me (with Python 3.4).

You could redefine the key by following:

if not 'keyname' in Dic:
     'keyname' = ""

If that was the reason it should work again.

delaflota
  • 159
  • 1
  • 3
  • 12
0

This error normally happens if you are using a version of Python less than 3.0. You need to ensure that the argument to the mutagen audio function is a Unicode string. So use

audio = EasyID3(C:\Users\Owner\Music\Music\Blue Öyster Cult\Blue Öyster Cult\Cities on Flame) print audio[U"artist"]

Jonathan
  • 2,635
  • 3
  • 30
  • 49