Python:[Errno 13]权限被拒绝
我目前正在尝试让一个随机笑话选择器/出纳员开始工作。我对Python有点陌生,但对其他语言有一些脚本编写经验。现在我将输入错误代码:Python:[Errno 13]权限被拒绝,python,errno,Python,Errno,我目前正在尝试让一个随机笑话选择器/出纳员开始工作。我对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:\User
> 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'
希望有人能帮助我确保您尝试访问的文件对执行python的用户具有正确的执行权限。您应该更改文件“audio.mp3”的权限。
使用shell命令检查文件的权限
ls-l
,并使用chmod a+rw audio.mp3
使文件可写。您可以找到关于类似问题的问题audio.mp3是否已打开?也许在另一个程序中?我很确定错误来自操作系统,这不是Python的问题。由于您的文件名没有路径,我假设您的库正在尝试将其写入某个默认位置,并且由于某些原因,这是不允许的。也许您可以尝试将文件名作为完全限定的路径名字符串传递,看看是否有效。这似乎是目录权限的问题-您应该只写入临时目录或用户目录。
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())