here\'s my function that creates file on the fly(when the user clicks proper link)
@app.route(\'/survey//report\')
def survey_downloadreport
I've used os.unlink for a while with success:
import os
os.unlink(os.path.join('/path/files/csv/', '%s' % file))
Hope it helps.
On Linux, if you have an open file you can still read it even when deleted. Do this:
import tempfile
from flask import send_file
csvf = tempfile.TemporaryFile()
wr = csv.DictWriter(csvf, fields, encoding = 'cp949')
wr.writerow(dict(zip(fields, fields))) #dummy, to explain what each column means
for resp in resps :
wr.writerow(resp)
wr.close()
csvf.seek(0) # rewind to the start
send_file(csvf, as_attachment=True, attachment_filename='survey.csv')
The csvf
file is deleted as soon as it is created; the OS will reclaim the space once the file is closed (which cpython will do for you as soon as the request is completed and the last reference to the file object is deleted). Optionally, you could use the after_this_request hook to explicitly close the file object.