Python 在使用语音助手时,是否有解决PermissionError:[Errno13]的方法?

Python 在使用语音助手时,是否有解决PermissionError:[Errno13]的方法?,python,python-3.x,errno,Python,Python 3.x,Errno,我目前正在用python进行一个语音项目。该程序工作正常,直到我在程序说“hello User”之后说“hello”。它会给我以下错误: C:\Users\HP\AppData\Local\Programs\Python\Python38-32\python.exe C:/Users/HP/.PyCharmCE2019.3/config/scratches/scratch_1.py hello Traceback (most recent call last): File "C:/

我目前正在用python进行一个语音项目。该程序工作正常,直到我在程序说“hello User”之后说“hello”。它会给我以下错误:

    C:\Users\HP\AppData\Local\Programs\Python\Python38-32\python.exe 
C:/Users/HP/.PyCharmCE2019.3/config/scratches/scratch_1.py
hello
Traceback (most recent call last):
  File "C:/Users/HP/.PyCharmCE2019.3/config/scratches/scratch_1.py", line 39, in <module>
    speak("Hello, how are you?")
  File "C:/Users/HP/.PyCharmCE2019.3/config/scratches/scratch_1.py", line 16, in speak
    tts.save(randFile)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python38-32\lib\site-packages\gtts\tts.py", line 294, in save
    with open(str(savefile), 'wb') as f:
PermissionError: [Errno 13] Permission denied: '91252543randomtext98303551.mp3'
from gtts import gTTS
import time
import speech_recognition as sr
import playsound
import os
import random

r1 = random.randint(1, 100000000)
r2 = random.randint(1, 100000000)
randFile = str(r1) + "randomtext" + str(r2) + ".mp3"


def speak(text):
    tts = gTTS(text=text, lang="en")
    tts.save(randFile)
    playsound.playsound(randFile)


speak("Hello User!")
os.remove(randFile)


def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        audio = r.listen(source)
        said = ""

        try:
            said = r.recognize_google(audio)
        except Exception as e:
            print("Sorry, my speech service is down!")
   return said


    if text == 'hello':
        speak("Hello, how are you?")

    if text == 'what is your name?':
        speak("My name is Alex")

我已经看到了几十种解决这个错误的方法,也尝试过用“randFile”保存音频文件。然而,并没有一种方法是有用的

您正试图将文件保存在您没有写入权限的目录中。尝试使用
tempfile
模块创建一个临时文件。@Barmar。我尝试使用temp文件,但我得到了以下错误:回溯(最近一次调用):文件“C:/Users/HP/Documents/PycharmTest/test2.py”,在speak(“Hello User!”)中的第26行;文件“C:/Users/HP/Documents/PycharmTest/test2.py”,在speak tts.save(temp)文件中的第22行“C:\Users\HP\AppData\Local\Programs\Python38-32\lib\site packages\gtts\tts.py”,第294行,以打开方式保存(str(savefile),'wb')作为f:OSError:[Errno 22]无效参数:“”
tts.save()
确保使用
NamedTemporaryFile
@Barmar。我在函数speak外部保存了randFile。在函数内部保存了randFile后,问题就解决了。非常感谢您的帮助!