0

I am currently trying to get a random joke selector/ teller to get to work. I am kinda new to Python but have some scripting experience with other languages. I will now put the error Code:

> 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'

I will now show you my source code, Also, you might find my strings weird, they're in german since i am from Germany

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())

hope someone can help me

drkater
  • 1
  • 1
  • 1
  • Is audio.mp3 already open? Perhaps in another program? – Acccumulation Oct 18 '18 at 21:30
  • I am pretty sure the error is from the operating system, and it's not a Python problem. Since your filename doesn't have a path I assume that your library is trying to write it to some default location and for some reason that is not permitted. Perhaps you could trying passing the filename as a fully qualified pathname string and see if that works. – Paul Cornelius Oct 18 '18 at 21:33
  • This looks like a problem with the *directory* permissions - you should only ever write to either the temp directory or the user directory. – o11c Oct 18 '18 at 22:18

2 Answers2

1

You should change the permission on you file 'audio.mp3'. Check the permissions on your file with the shell commande ls -l and use chmod a+rw audio.mp3 to make your file writable. You can find a question on a similar problem here

dallonsi
  • 1,299
  • 1
  • 8
  • 29
0

Make sure the file you are trying to access has the correct execution permissions for the user executing python.

bbot
  • 31
  • 3