python使用语音识别后可以´;不要删除音频文件

python使用语音识别后可以´;不要删除音频文件,python,operating-system,speech-recognition,Python,Operating System,Speech Recognition,这是我的语音到文本的测试代码,speec到文本的工作唯一的问题是删除音频文件后 import os from os import walk import speech_recognition as sr def remove_spaces(string) : # removes all spaces in a string return string.replace(" ", "") def search_name() : # sea

这是我的语音到文本的测试代码,speec到文本的工作唯一的问题是删除音频文件后

   import os
from os import walk
import speech_recognition as sr


def remove_spaces(string) : # removes all spaces in a string
    return string.replace(" ", "")


def search_name() : # search for any file in path
    _, _, filenames = next(walk('C:\\audios\\')) 
    return filenames[0]


def remove_file(name) : # should look for the file and removes it 
    print(('C:\\audios\\' + name))
    if os.path.isfile(('C:\\\audios\\' + name)) :
        os.remove(('C:\\audios\\' + name))
        print("success, file .waw removed!")
    else :
        print("File doesn't exists!")


def main() :
    r = sr.Recognizer()  
    audio_name = search_name() #get the name of file
    print("Filename of Audio:", audio_name)
    with sr.AudioFile(f'C:\\audios\\{audio_name}') as source :
        audio_text = r.listen(source)
        text = r.recognize_google(audio_text, language='de-DE')
        print('Converting audio transcripts into text ...')
        code = remove_spaces(text)
        print(code)
        remove_file(audio_name)
        return code


if __name__ == '__main__' :
    main()
错误是,在使用speech to text后,我想删除'audio.waw',但它说该文件仍在使用中

    C:\Users\J-Roc\AppData\Local\Programs\Python\Python39\python.exe C:/speech_to_text.py
Filename of Audio: xyz.wav
Traceback (most recent call last):
  File "C:\speech_to_text.py", line 43, in <module>
    main()
  File "C:\speech_to_text.py", line 38, in main
    remove_file(audio_name)
  File "C:\speech_to_text.py", line 20, in remove_file
    os.remove(('C:\\audios\\' + name))
PermissionError: [WinError 32] The process cannot access the file because it is used by another process: 'C:\\audios\\xyz.wav'
Converting audio transcripts into text ...
5549355
C:\audios\xyz.wav

Process finished with exit code 1
C:\Users\J-Roc\AppData\Local\Programs\Python\Python39\Python.exe C:/speech\u to\u text.py
音频文件名:xyz.wav
回溯(最近一次呼叫最后一次):
文件“C:\speech\u to\u text.py”,第43行,在
main()
文件“C:\speech\u to\u text.py”,第38行,在main中
删除\u文件(音频\u名称)
文件“C:\speech\u to\u text.py”,第20行,在remove\u文件中
os.remove(('C:\\audios\\\'+名称))
PermissionError:[WinError 32]该进程无法访问该文件,因为另一进程正在使用该文件:“C:\\audios\\xyz.wav”
正在将音频转录本转换为文本。。。
5549355
C:\audios\xyz.wav
进程已完成,退出代码为1

您正在删除文件上下文中的文件。 您应该在上下文完成后删除。 在您的主要功能中:

with sr.AudioFile(f'C:\\audios\\{audio_name}') as source :
        audio_text = r.listen(source)
        text = r.recognize_google(audio_text, language='de-DE')
        print('Converting audio transcripts into text ...')
        code = remove_spaces(text)
        print(code)
        remove_file(audio_name) #Still inside the file context. Can't remove file. 
remove_file(audio_name)
return code

另外,我们也非常鼓励使用try-except。

现在对我有效thx:)


thx,我之前尝试过这个,但是没有成功。问题是:D中的“返回码”
  def main() :
    r = sr.Recognizer()
    audio_name = search_name()
    print("Filename of Audio:", audio_name)
    with sr.AudioFile(f'C:\\ms_acc_creator_\\audios\\{audio_name}') as source :
        audio_text = r.listen(source)
        text = r.recognize_google(audio_text, language='de-DE')
        print('Converting audio transcripts into text ...')
        code = remove_spaces(text)
        print(code)

    remove_file(audio_name)
    return code


if __name__ == '__main__' :
    main()