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