2016-10-02 212 views
0

写入音频文件时出现错误。Errno 13权限被拒绝:'file.mp3'Python

基本上,我覆盖了mp3中的数据,只要我的函数被调用,然后播放它。

它的作品第一次通过,但随后给我[Errno 13] Permission denied: 'file.mp3'然后。

下面是代码:

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

错误是在tts.save线。

更多信息:

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: 

谢谢!

+1

也许程序(播放它)阻止访问文件的概率。 – furas

+0

会有一种方法来测试吗? – Jaromando

+0

你是否以管理员身份启动python脚本? Windows用户有权访问此文件吗? 这里验证进程访问的文件或文件夹的方式http://superuser.com/questions/117902/find-out-which-process-is-locking-a-file-or-folder-in-windows 也你在脚本之前打开这个文件吗? –

回答

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

每次保存时都要更改文件名,这对我很有用。

0

一个更好的解决方案是这样的,如果我有时间我还要计算有史以来登陆同一个文件

from gtts import gTTS 
from playsound import playsound 
import random 
import os 

#creating a super random named file 

r1 = random.randint(1,10000000) 
r2 = random.randint(1,10000000) 

randfile = str(r2)+"randomtext"+str(r1) +".mp3" 

tts = gTTS(text='hey, STOP It!!', lang='en', slow=True) 
tts.save(randfile) 
playsound(randfile) 

print(randfile) 
os.remove(randfile) 
相关问题