2017-02-22 119 views
-1

我想在Python中選擇一個文件夾中的隨機音樂文件使用Windows命令:random.choice()os.listdir()os.startfile()os.startfile說文件不存在,但它呢?

下面是代碼:

import os, random song = random.choice(os.listdir("C:\Users\MASONF\Music\Downloaded")) os.startfile(song)

它返回錯誤

Traceback (most recent call last): File "C:\Users\MASONF\Desktop\successfuly chosen random file non existent.py", line 3, in <module> os.startfile(song) WindowsError: [Error 2] The system cannot find the file specified: 'Panda Eyes - Drippy Dub.mp3'

該文件存在但它找不到它?我是新來的Python,不知道任何東西很多命令,所以我可能已經錯過了一些明顯的

回答

0

您需要的基本路徑添加到歌曲名稱:

import os, random 
path = r"C:\Users\MASONF\Music\Downloaded" 
song = random.choice(os.listdir(path)) 
os.startfile(path+'\\'+song) 
相關問題