How do I delete a file despite being used by a process?

0

I am developing a bot for telegram, one of its functions is the possibility of sharing a video of youtube and downloading it.

Using a library called "PAFY" documentation here I have managed to make it possible to download a YouTube video from of a link and send it via Telegram. My problem is this - >

-When the video is downloaded and sent to the user, said video is downloaded to my computer, in my folder where my bot.py is hosted. My idea is when the video is sent, eliminate it with os.remove. The problem with this is that it throws the exception [WinError 32] El proceso no tiene acceso al archivo porque está siendo utilizado por otro proceso: 'The Real Pencorder.mp4'

    if pattern.match(message.text):
    bot.send_message(message.chat.id, "Es un link de youtube valido")
    vid = pafy.new(message.text)
    s = vid.getbest()
    video = open(s.download(), 'rb')
    bot.send_video(message.chat.id,video)
    path = vid.title+'.mp4'
    os.remove(path)

My question is. How can I eliminate this video even though there are processes that use it? I have to say that I use Windows 10

    
asked by kimbo 21.05.2018 в 22:05
source

1 answer

0

My problem has been answered in the comments. I missed the exception

[WinError 32] El proceso no tiene acceso al archivo porque está siendo utilizado por otro proceso: 'The Real Pencorder.mp4'

Because my program had not closed the flow with this video. Using "Video.close ()" resolves the problem and the video can be deleted.

    
answered by 21.05.2018 / 22:15
source