thkang
thkang

Reputation: 11543

How can I generate file on the fly and delete it after download?

here's my function that creates file on the fly(when the user clicks proper link)

@app.route('/survey/<survey_id>/report')
def survey_downloadreport(survey_id):
    survey, bsonobj = survey_get(survey_id) #get object
    resps = response_get_multi(survey_id) #get responses to the object

    fields = ["_id", "sid", "date", "user_ip"] #meta-fields
    fields.extend(survey.formfields) #survey-specific fields

    randname = "".join(random.sample(string.letters + string.digits, 15)) + ".csv" #some random file name

    with open("static//" + randname, "wb") as csvf:
        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)

    return send_from_directory("static", randname, as_attachment = True)

I'd like to have file to be deleted after completing of the download. How can I do it?

Upvotes: 7

Views: 6467

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1123410

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.

Upvotes: 7

mjpirez
mjpirez

Reputation: 7

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.

Upvotes: -1

Related Questions