flask remove file after send file

Solutions on MaxInterview for flask remove file after send file by the best coders in the world

showing results for - "flask remove file after send file"
Florencia
12 May 2017
1@app.route('/files/<filename>/download')
2def download_file(filename):
3    file_path = derive_filepath_from_filename(filename)
4    file_handle = open(file_path, 'r')
5    @after_this_request
6    def remove_file(response):
7        try:
8            os.remove(file_path)
9            file_handle.close()
10        except Exception as error:
11            app.logger.error("Error removing or closing downloaded file handle", error)
12        return response
13    return send_file(file_handle)
14