GatoradeLover123
GatoradeLover123

Reputation: 33

Python- What does 'Error: AudioFileOpen failed ('wht?')' mean?

I am trying to run this code but it causes the following error: Error: AudioFileOpen failed ('wht?'). So here's my code-it's running on a Mac and with a folder of mp3 files if that is any help:

import os
def randomShuffleSongFromFolder(folderPath):
    try:
        music = os.listdir(folderPath)
    except:
        print('Error-Music folder not found')
        exit()
    random.shuffle(music)
    music.remove('.DS_Store')
    print(folderPath)
    print(music)
    for song in music:
        os.system('afplay "' + song + '"')
if __name__ == '__main__':
    print(os.listdir('/Users/isaac_lims_macbook_air/Desktop/davidMusic'))
randomShuffleSongFromFolder('/Users/isaac_lims_macbook_air/Desktop/MusicExample/')

Any help is welcomed and greatly thanked

Upvotes: 0

Views: 4459

Answers (1)

Chris
Chris

Reputation: 136880

Have a look at this (from your question):

for song in music:
    os.system('afplay "' + song + '"')

and remember what music contains: a list of file names (e.g. "Adele - Hello.mp3"). When you pass them to afplay you aren't telling it which directory to look in. (Note that the error message is not coming from Python, but rather from afplay itself; e.g. see this other question where the same command is called from C.)

Try using os.path.join() to include the directory, e.g.

import os.path

# Assuming folderPath is being passed into the containing function as above
for song in music:
    songPath = os.path.join(folderPath, song)
    os.system('afplay "{}"'.format(songPath))

That way afplay will receive an absolute path like "/Users/you/Desktop/Music/Adele - Hello.mp3".

Upvotes: 2

Related Questions