【发布时间】:2020-03-24 22:24:26
【问题描述】:
我目前正在尝试让一个随机的笑话选择器/出纳员开始工作。我对 Python 有点陌生,但对其他语言有一些脚本编写经验。我现在将错误代码:
> Traceback (most recent call last):
File "C:\Users\drkater\Desktop\Crap\Projekte\Voice Assitant\Jarvis.py", line 64, in <module>
assistant(myCommand())
File "C:\Users\drkater\Desktop\Crap\Projekte\Voice Assitant\Jarvis.py", line 59, in assistant
talkToMe('ich weis nicht was du meinst!')
File "C:\Users\drkater\Desktop\Crap\Projekte\Voice Assitant\Jarvis.py", line 23, in talkToMe
text_to_speech.save('audio.mp3')
File "C:\Python\lib\site-packages\gtts\tts.py", line 246, in save
with open(savefile, 'wb') as f:
PermissionError: [Errno 13] Permission denied: 'audio.mp3'
我现在将向您展示我的源代码,另外,您可能会发现我的字符串很奇怪,因为我来自德国,所以它们是德语
from gtts import gTTS
from playsound import playsound
from random import randint
import speech_recognition as sr
import os
import re
import webbrowser
import smtplib
import requests
jokes = [
"Wie viel wiegt ein Hipster?, ein instagram",
"Wer hätte gedacht, dass das Leben als Informatiker so Hardware",
"Der Postbote geht von Schlitz zu Schlitz bis der Sack leer ist!",
]
def talkToMe(audio):
print(audio)
if os.path.isfile('./audio.mp3'):
os.remove('./audio.mp3')
text_to_speech = gTTS(text=audio, lang='de')
text_to_speech.save('audio.mp3')
playsound('audio.mp3')
def myCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print('Bereit...')
r.pause_threshold = 1
r.adjust_for_ambient_noise(source, duration=1)
audio = r.listen(source)
try:
command = r.recognize_google(audio).lower()
print('Du sagtest: ' + command + '\n')
except sr.UnknownValueError:
print('dein letzter befehl war undeutlich!')
command = myCommand();
return command
def assistant(command):
if 'open youtube' in command:
reg_ex = re.search('öffne youtube (.*)', command)
url = 'https://www.youtube.com/'
webbrowser.open(url)
elif 'tell me a joke' in command:
talkToMe(jokes[randint(0, len(jokes) - 1)])
else:
talkToMe('ich weis nicht was du meinst!')
talkToMe('warte auf weitere befehle')
while True:
assistant(myCommand())
希望有人可以帮助我
【问题讨论】:
-
audio.mp3 已经打开了吗?也许在另一个程序中?
-
我很确定错误来自操作系统,而不是 Python 问题。由于您的文件名没有路径,我假设您的库正在尝试将其写入某个默认位置,并且出于某种原因,这是不允许的。也许您可以尝试将文件名作为完全限定的路径名字符串传递,看看是否可行。
-
这看起来像是 directory 权限的问题 - 您应该只写入临时目录或用户目录。