我正在尝试运行此代码,但它会导致以下错误:错误:AudioFileOpen失败('wht?')。所以这是我的代码 - 它在Mac上运行,如果有任何帮助,还有mp3文件的文件夹:
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 :(得分:0)
看看这个(来自你的问题):
for song in music:
os.system('afplay "' + song + '"')
并记住music
包含的内容:文件名列表(例如"Adele - Hello.mp3"
)。当您将它们传递给afplay
时,您无法告诉它要查看哪个目录。(请注意,错误消息不是来自Python,而是来自afplay
本身;例如,请参阅{{ 3}}。)
尝试使用this other question where the same command is called from C来包含目录,例如
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"
的绝对路径。