Search code examples
pythongtts

how to use gtts repeatedly?


when i call my function once i have no errors but if i call it repeatedly i will have the following error :

Exception has occurred: PermissionError [Errno 13] Permission denied: 'tolkback.mp3'

from gtts import gTTS
import pyglet
from playsound import playsound


def TalkBack(case_ans):
    print("in ...................................")
    tts = gTTS(case_ans)

    tts.save('tolkback.mp3')

    print("saving...............................")
    playsound('tolkback.mp3')
    print("saying................................")


TalkBack("my bad end 1")
TalkBack("go go end 2")
TalkBack("find me my self end 3")
TalkBack("games maker end 4")
TalkBack("say my name end 5")

the program should talk back the text


Solution

  • this is the solution that i made >> with the Gide of my friend furas

    from playsound import playsound
    import webSearch
    import os
    import random
    
    def name_generator():
        ran = random.randint(1,5000)
        ran = str(ran)
        return ran
    
    
    def TalkBack(case_ans):
        print("in ...................................")
        tts = gTTS(case_ans)
        new_name = name_generator()
        new_name= new_name+".mp3"
        tts.save(new_name)
    
        print("saving...............................")
        playsound(new_name)
        print("saying................................")
        try:
            os.remove(new_name) 
        except:
            print("i cant")
    
    
    
    TalkBack("my bad end 1")
    TalkBack("go go end 2")
    TalkBack("find me my self end 3")
    TalkBack("games maker end 4")
    TalkBack("say my name end 5")
    

    generating a new .mp3 file with a random name and deleting it after using it