【问题标题】:PermissionError: [Errno 13] Permission denied: 'voice.mp3'PermissionError:[Errno 13] 权限被拒绝:'voice.mp3'
【发布时间】:2020-06-06 21:16:02
【问题描述】:

我正在尝试在 Python 中创建一个 ChatBot,VA,用于接收音频输出

代码如下:

from gtts import gTTS
import speech_recognition as sr
import playsound
import os #to save the file
import time


def speak(text):#this defines the function SPEAK and creates the command for its use
           tts = gTTS(text=text, lang="en")
           filename = "voice.mp3"
           tts.save(filename) #saves the audio file
           playsound.playsound(filename)

       #function ends here

要播放音频,我按以下方式使用该功能:

>>>speak("I am speaking this text")

上面的 ^^^ 工作得很好,但是当我使用多个语句时:

>>>speak("I am saying this")
>>>speak("And also this!")

它给出了一个错误:

PermissionError: [Errno 13] Permission denied: 'voice.mp3'

我认为这是由于音频文件的名称匹配。但是当我尝试使用循环每次更改数字时,在播放音频指定次数后仍然显示错误。有人可以给我一个解决方案吗?谢谢!

【问题讨论】:

    标签: python bots chatbot


    【解决方案1】:

    您不能通过函数的不同调用覆盖文件voice.mp3。您需要做的是在播放文件后删除 voice.mp3 文件。所以:

    def speak(text):#this defines the function SPEAK and creates the command for its use
           tts = gTTS(text=text, lang="en")
           filename = "voice.mp3"
           tts.save(filename) #saves the audio file
           playsound.playsound(filename)
           os.remove(filename)
    

    确保您没有将文件存储在敏感位置,并且您的工作目录位于文档或下载或其他内容中的某个位置。

    【讨论】:

    • 感谢您花时间回答这个问题,而我正在研究我发现这种方法并且它有效。
    • 太棒了!也很高兴帮助
    【解决方案2】:

    函数运行时听起来像是文件权限问题,它正在覆盖现有文件

    我会检查运行代码的用户/组是否有读/写权限

    这取决于你使用的是 windows、mac 还是 linux

    【讨论】:

      猜你喜欢
      • 2015-07-17
      • 2016-07-25
      • 2018-11-18
      • 2020-07-01
      • 2016-11-12
      • 2019-11-09
      • 2021-11-11
      • 2020-02-27
      • 2017-10-25
      相关资源
      最近更新 更多