Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errno 13 Permission denied: 'file.mp3' Python

I am getting an error when writing to an audio file.

Basically, I am overwriting the data in the mp3 whenever my function gets called and then playing it.

It works the first time through, but then gives me [Errno 13] Permission denied: 'file.mp3' then on.

Here is the code:

def speech(self, response):
    audio_file = "response.mp3"
    tts = gTTS(text=str(response), lang="en")
    tts.save(audio_file)
    pygame.mixer.init()
    pygame.mixer.music.load(audio_file)
    pygame.mixer.music.play()

Error is on the tts.save line.

More info:

Traceback (most recent call last):

  File "myprojectpath", line 63, in speech
    tts.save(audio_file)

  File "C:\Python35\lib\site-packages\gtts\tts.py", line 93, in save
    with open(savefile, 'wb') as f:

Thanks!

like image 362
Jaromando Avatar asked Sep 18 '25 16:09

Jaromando


2 Answers

from gtts import gTTS
import playsound
import os

x = ['sunny', 'sagar', 'akhil']
tts = 'tts'
for i in range(0,3):
    tts = gTTS(text= x[i], lang = 'en')
    file1 = str("hello" + str(i) + ".mp3")
    tts.save(file1)
    playsound.playsound(file1,True)
    print 'after'
    os.remove(file1)

change the file name every time you save it, this worked for me.

like image 112
Sunny Avatar answered Sep 21 '25 04:09

Sunny


You should name your file for a unique name each time , so the solution that I realized is to name your file with date like this ==>

    date_string = datetime.now().strftime("%d%m%Y%H%M%S")
    filename = "voice"+date_string+".mp3"
    tts.save(filename)
    playsound.playsound(filename)
like image 44
Firas Ben Younes Avatar answered Sep 21 '25 05:09

Firas Ben Younes