2017-04-16 35 views
-3

我試圖運行此代碼,但它導致以下錯誤:錯誤:AudioFileOpen失敗('wht?')。因此,這裏是我的代碼,它是在Mac上並與MP3文件的文件夾運行,如果這是任何幫助:Python-錯誤:AudioFileOpen失敗('wht?')'是什麼意思?

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/') 

任何幫助,歡迎和極大的感謝

+0

它可能來自'playMusic()',但我們無法知道該函數的功能。這絕對不是來自標準庫。 – Chris

回答

0

看一看這(從你的問題):

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

並記住music包含:(文件名列表如"Adele - Hello.mp3")。當你把它們傳遞給afplay你不告訴它在尋找哪個目錄(請注意,該錯誤信息不會從Python中來,而是從自身afplay;如見this other question where the same command is called from C。)

嘗試使用os.path.join()包括該目錄,例如

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)) 

這樣afplay會收到類似"/Users/you/Desktop/Music/Adele - Hello.mp3"的絕對路徑。