Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to close file after request

files = {'file': ('output.mp3', open('output.mp3','rb'), 'audio/mpeg')}

I am using this for POST request but after usage trying to delete it with os.remove and it gives "it’s used by a process". How to close file after?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use with ...:

import os
import requests

# open the file and send it
with open("output.mp3", "rb") as my_file:
    files = {"file": ("output.mp3", my_file, "audio/mpeg")}
    r = requests.post(url, files=files)
    # ...

# file is closed now, remove it
os.remove("output.mp3")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading