raise MutagenError(e) mutagen.MutagenError: [Errno 2] No such file or directory: "audio.mp3"

Good day! I’ve created a desktop assistant that can play music for me through speech-recognition. This is my code:

while True:
                with sr.Microphone() as src:
                    audio5 = recog1.listen(src, timeout=1, phrase_time_limit=5) # listens to what I will say(basically a song title)

                try:
                    for files in os.listdir("C:\\Users\\Allexus\\Downloads"): # iterate in each file in Downloads folder
                        if recog1.recognize_google(audio5, language="en-US") in files: # check if what i said above(which is "tonight") is found in the files in my Downloads folder
                            print("Now playing " + os.path.basename(files)) # THIS WORKS PERFECTLY
                            engine.say("Now playing " + os.path.basename(files)) # THIS WORKS PERFECTLY
                            engine.runAndWait()

                          # music = "im with you.mp3"
                            files_type = os.path.splitext(files)

                            if files_type[1] == ".mp3":
                                music = MP3(files)
                                mixer.music.load(music)
                                mixer.music.play()

                                while mixer.music.get_busy():
                                    time.Clock().tick(10)

 but it’s giving me this error even though the .mp3 file exist in the directory:

raise MutagenError(e)mutagen.MutagenError: [Errno 2] No such file or directory: ‘tonight_3d.mp3.mp3’

If you need more info about anything please ask me and I will provide it. Thank you so much.

os.listdir returns the filenames within the directory, NOT the full path. So MP3(files) is probably falling over because it is trying to play “tonight_3d.mp3.mp3” in your current working directory (i.e. where you are running the program). If you were running your program in the downloads folder, it’d probably work. [You can verify this with a print of os.getcwd()].

I expect you need to pass MP3() the full path, i.e.

MP3(os.path.join(<path to downloads>,files))

where “<path to downloads>” is the full path to your downloads folder.

You might also consider changing your search for “.mp3” to:

filename, file_extension = os.path.splitext(files)
if (file_extension == ".mp3")

as any file like “tonight_3d.good.mp3” will fail your current test.