【问题标题】:Errno 13 Permission denied: 'file.mp3' PythonErrno 13 权限被拒绝:'file.mp3' Python
【发布时间】:2017-02-10 15:35:10
【问题描述】:

写入音频文件时出现错误。

基本上,每当我的函数被调用然后播放时,我都会覆盖 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:

谢谢!

【问题讨论】:

  • 可能是程序(播放它)阻止了对文件的访问。
  • 有办法测试吗?
  • 您是否以管理员身份启动 python 脚本? Windows 用户是否有权访问此文件?这里是验证访问文件或文件夹superuser.com/questions/117902/…的进程的方法你之前是否在脚本中打开过这个文件?
  • 我不相信这些都是问题,因为我尝试在管理员中运行 Pycharm,如果它至少执行一次,它显然可以访问文件并写入它,我从来没有真正打开它。唯一一次编辑是在提供的代码中。
  • 在尝试重写文件之前,您是否曾经调用过 pygame.mixer.music.stop()?如果 pygame 仍然打开文件以读取它,则可能会在您尝试覆盖文件时产生干扰。

标签: python pygame mp3 text-to-speech


【解决方案1】:

尝试保存fi打开另一个不播放的文件,然后您将有权删除它这里是谈话功能:

def Talk (mytext, language= "fr" ):
    myobj = gTTS(text=mytext, lang=language, slow=False)
    myobj.save("1.mp3")
    mixer.init()
    mixer.music.load("1.mp3")
    mixer.music.set_volume(1)
    mixer.music.play()
    mixer.music.get_endevent()
    while mixer.music.get_busy():
        continue
    mixer.music.load("Talker\empty.mp3")
    os.remove("1.mp3")

【讨论】:

    【解决方案2】:

    如果您不想再保存之前的文件,您可以在保存下一个文件之前删除音频文件。因此,您可以使用相同的名称保存新文件。 下面是我运行成功的代码

    import speech_recognition as ram
    from playsound import playsound
    from gtts import gTTS
    import os
    r=ram.Recognizer()
    with ram.Microphone() as source:
        while True:
            r.adjust_for_ambient_noise(source,duration=1)
            print("I'm Listening....")
            playsound('audio/listen.mp3')
            audio=r.listen(source, timeout=5)
            text=r.recognize_google(audio)
            tts = gTTS("Boss. you said "+str(text)+". i'm opening it")
            tts.save('audio/text.mp3')
            print("you said "+text+".")
            playsound('audio/text.mp3')
            os.remove('audio/text.mp3')
    

    【讨论】:

      【解决方案3】:

      我也遇到了同样的问题,然后发现文件指向之前的位置,所以我必须释放它。它工作正常,重要的是它也不会创建 mp3 文件。如果不释放,第一次可以正常工作并保存音频文件,然后第二次出现错误。

      from gtts import gTTS
      import  os
      import playsound
      
      def tts(text, lang):
           file = gTTS(text = text, lang = lang)
           file.save("audio/speak_func.mp3")
           playsound.playsound('audio/speak_func.mp3', True)
           os.remove("audio/speak_func.mp3")
      

      【讨论】:

        【解决方案4】:

        您收到此错误是因为混音器未释放该音频文件。如果您想混音器释放该文件,您可以将无用或空文件加载到混音器中。然后您可以覆盖该音频文件。 例如..Mixer.load("C:/...../empty.mp3") 在此之前加载的文件被释放后。

        【讨论】:

          【解决方案5】:

          你应该每次都为你的文件命名一个唯一的名字,所以我意识到的解决方案是用 date 像这样 ==>

              date_string = datetime.now().strftime("%d%m%Y%H%M%S")
              filename = "voice"+date_string+".mp3"
              tts.save(filename)
              playsound.playsound(filename)
          

          【讨论】:

            【解决方案6】:

            您可以在同一文件夹中创建另一个文件,只需复制并重命名即可。

            您要使用的文件:("C:/.../file.mp3") 复制的文件:("C:/.../file_copy.mp3")

            您可以将mixer.music.load("C:/.../file.mp3") 中加载的文件更改为mixer.music.load("C:/.../file_copy.mp3") 和使用这个删除 ("C:/.../file.mp3") 没有问题:

            from pygame import mixer
            import os
            
            mixer.music.load("C:/.../file.mp3")
            mixer.music.play()
            mixer.music.load("C:/.../file_copy.mp3")
            os.remove("C:/.../file.mp3")
            

            但是上面的代码会在播放之前删除文件,所以你可以使用一个方法来等待它播放:

            from pygame import mixer
            import os
            
            mixer.music.load("C:/.../file.mp3")
            mixer.music.play()
            while mixer.music.get_busy(): # check if the file is playing
                pass
            mixer.music.load("C:/.../file_copy.mp3")
            os.remove("C:/.../file.mp3")
            

            【讨论】:

              【解决方案7】:

              这是一个更好的解决方案,如果我有时间,我还会计算登陆同一个文件的概率

              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)
              

              【讨论】:

                【解决方案8】:
                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)
                

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

                【讨论】:

                • 有没有办法在不更改文件名的情况下做到这一点??
                猜你喜欢
                • 2016-07-02
                • 2017-04-20
                • 2021-05-18
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-05-31
                • 1970-01-01
                相关资源
                最近更新 更多