Python:[Errno 13]权限被拒绝

时间:2018-10-18 21:22:16

标签: 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:\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())

希望有人可以帮助我

2 个答案:

答案 0 :(得分:0)

确保您要访问的文件对执行python的用户具有正确的执行权限。

答案 1 :(得分:0)

您应该更改文件'audio.mp3'的权限。 使用shell命令for table in Session.tables: for column in table.columns: if column.type == Numeric: column.type = float 检查文件的权限,并使用ls -l使文件可写。您可以找到关于类似问题here

的问题
相关问题