我创建了一个虚拟助手,一切都很好,但现在在我安装了api之后,我的音乐功能无法获取位置

2024-06-12 03:43:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个虚拟助理音乐播放之前,在我安装了一个api“zolframalpha”后,错误发生在播放音乐我附加了错误代码

错误是:

line 1 :-----File "C:\Users\Hari Prakash\Desktop\siri\main.py", line 62, in playMusic      os.startfile(location+musics[0]) 

line2:--------- FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Users\\Hari Prakash\\Desktop\\siri\\musics//wolframalpha.cpython-37.pyc' 
def playMusic(self):
    self.speak('Playing Music Please Wait')
    musics = [x for x in os.walk(os.getcwd())][-1][-1]
    shuffle(musics)
    location = os.path.join(
        os.getcwd(),
        "musics//"  
    )
    os.startfile(location+musics[0])
    print(musics)
    self.main()

Tags: inself音乐osmain错误linelocation
1条回答
网友
1楼 · 发布于 2024-06-12 03:43:29

线路

musics = [x for x in os.walk(os.getcwd())][-1][-1]

获取当前工作目录中最后一个子目录中的文件列表

假设您的音乐位于当前工作目录的“musics”文件夹中,您可以像这样对代码重新排序,并更改musics =的行

def playMusic(self):
    self.speak('Playing Music Please Wait')
    location = os.path.join(
        os.getcwd(),
        "musics//"  
    )
    # get all files in musics location
    musics = [x for x in os.listdir(location) if os.path.isfile(location + x)]
    shuffle(musics)

    os.startfile(location+musics[0])
    print(musics)
    self.main()

相关问题 更多 >