Search code examples
pythondirectoryeyed3

Trying to access id3 tags of mp3 songs from a folder using a loop


I am trying to access the metadata of all mp3 songs in a folder. I'm working on Ubuntu 16.04 Virtualbox and eyed3 0.6.18-1 with a shared folder between windows and ubuntu. I tried the following:

import os
import eyeD3

for root, dir, files in os.walk("home/undead/ShareWindowsTest")":
   for file in files:
      if file.endswith(".mp3"):
          audiofile = eyeD3.Mp3AudioFile(file)
          print audiofile.tag.getTitle()

Which is supposed print the title of each song in the folder. However this does not work. I tried using

if eyed3.isMp3File(file): 

But still no luck. The error is specifically in the audiofile=... line where "file" seems to be a string and not an mp3 file, thus not being a proper input argument. I'm really new to this and would appreciate some help.


Solution

  • Now that you typed the full error message the error is clear:

    No such file or directory: '01 California Dreaming (copy).mp3'
    

    That means that the call is correct but the file name you are using does not exist. You are thinking: "but it came from os.walk() so it must exist!". Well yes, but os.walk() returns a list of tuples, each of three values: root, dirs, files. The files list of names are the filenames in the root directory.

    Solution: you have to concatenate the root and file names:

    for root, dir, files in os.walk("home/undead/ShareWindowsTest")":
        for file in files:
            path = os.path.join(root, file)
            audiofile = eyeD3.Mp3AudioFile(path)
    

    PS: Are you missing a leading / in your os.walk("/home...)?