2017-02-22 58 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) 
相关问题