Younes Gouyd
Younes Gouyd

Reputation: 23

AttributeError: 'NoneType' object has no attribute 'tag' (using eyed3)

I want to create a list of dictionaries with the keys: Title, Album, and Artist.

I get the error:

AttributeError: 'NoneType' object has no attribute 'tag'

line 17:

song_title  = mediafile.tag.title,

Here is my code:

import glob
import eyed3

class Song:
    def __init__(self, song_title, album, artist_name,):
        self.song_title  = song_title
        self.album       = album
        self.artist_name = artist_name

songs = []

media = glob.glob('C:\\My Stuff\\My Music (For Groove)/**/*.mp3', recursive=True)

for song in media:
    mediafile = eyed3.load(song)
    a = Song(
            song_title  = mediafile.tag.title,
            album       = mediafile.tag.album,
            artist_name = mediafile.tag.artist, 
        )
    songs.append({'Title' : a.song_title, 'Album' : a.album, 'Artist' : a.artist_name})

Any help will be appreciated.

Upvotes: 2

Views: 4940

Answers (2)

Beau
Beau

Reputation: 447

If you load a file that doesn't have a tag yet, you need to initialise it

f = eyed3.load(file)
if not f.tag:
    f.initTag()

Upvotes: 3

abolotnov
abolotnov

Reputation: 4332

From eyed3 documentation:

Loads the file identified by path and returns a concrete type of eyed3.core.AudioFile. If path is not a file an IOError is raised. None is returned when the file type (i.e. mime-type) is not recognized.

In your code, you are trying to extract the tags from the mediafile before you know it's not None and this is why you are getting this.

You could do a couple of things:

try/catch for IOError and check for None value of your mediafile if the file's content is not recognized:

try:
    mediafile = eyed3.load(song)
    if mediafile:
        ... extract tag
    else:
        ... log not recognized file format
except IOError:
    .... log Exception

Upvotes: 2

Related Questions