ConstantinL ConstantinL - 7 months ago 67
Python Question

Remove file after Flask serves it

I have a Flask view that generates data and saves it as a CSV file with Pandas, then displays the data. A second view serves the generated file. I want to remove the file after it is downloaded. My current code raises a permission error, maybe because

deletes the file before it is served with
. How can I delete a file after serving it?

def process_data(data)
tempname = str(uuid4()) + '.csv'
return file

def getcsv(file):
def cleanup(response):
os.remove('samo/static/temp/' + file)
return response

return send_from_directory(directory=cwd + '/samo/static/temp/', filename=file, as_attachment=True)


after_request runs after the view returns but before the response is sent. Sending a file may use a streaming response; if you delete it before it's read fully you can run into errors.

Read the file into memory and serve it, so that's it's not being read when you delete it later. In case the file is too big to read into memory, use a generator to serve it then delete it.

def download_and_remove(filename):
    path = os.path.join(current_app.instance_path, filename)

    def generate():
        with open(path) as f:
            yield from f


    r = current_app.response_class(generate(), mimetype='text/csv')
    r.headers.set('Content-Disposition', 'attachment', filename='data.csv')
    return r