2016-10-02 213 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) 
相關問題