python after using speech recognition can´t delete the audio file

Viewed 222

this is my testcode for speech to text, the speec to text works the only problem is to remove the audiofile after

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

the error, after using speech to text i would like to delete the 'audio.waw' but it says the file is still in use

    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
2 Answers

You are removing the file within the file context. You should remove after the context has finished. In your main function:

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

Also use of try-except is highly encouraged.

works for me now thx :)

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