I am trying to delete files after a user downloads them. In my app, the file name is passed from another script via a session variable.
The file structure is:
app
routes.py
--tmp<--location where files are downloaded and retrieved
This first snippet works: the file is successfully retrieved and downloaded, so I know the session variable is working.
def download_jrnlvl():
output_file1 = session.get('session_variable', None)
return send_file(output_file1, attachment_filename='jrnlvl_' + today + '.xlsx', as_attachment=True)
On a question I posted, I was referred to this answer and I have tried to apply the solution, using a generator to serve and then delete the file.
def download_outputfile():
output_file1 = session.get('output_file', None)
path = os.path.join(current_app.instance_path,output_file1)
def generate():
with open(path) as f:
yield from f
os.remove(path)
r = app.response_class(generate(), mimetype='text/xlsx')
r.headers.set('Content-Disposition', 'attachment', filename='outputfile1.xlsx')
return r
The file that downloads is empty.
Using Flask Debug Toolbar I see the following under 'Request Vars' and I'm not sure how to apply this information to my predicament. Attempting absolute paths like below doesn't work:
('message', 'E:\python\flask\pminsts_v2.5.3\app\instance\tmp/tmp/output_020218.xlsx'), ('message', 'E:/tmp/tmp/output_020218.xlsx'), ('message', 'tmp/tmp/output_020218.xlsx'), ('message', '/tmp/tmp/output_020218.xlsx'), ('message', '/app/tmp/tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/tmp/tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/tmp\tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/tmp\tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/instance/tmp\tmp/output_020218.xlsx'), ('message', 'E:\python\flask\pminsts_v2.5.3\app\instance\tmp/output_020218.xlsx'), ('message', 'E:\python\flask\pminsts_v2.5.3\app\instance\tmp/output_020218.xlsx'), ('message', 'E:/python/flask/pminsts_v2.5.3/app/tmp/output_combined_020218.xlsx')
In the code with the generator, I have tried an absolute path to the 'tmp' directory, and I am trying to get a handle on how to use instance_path. How do I get the correct path for the generator to work?
Grateful in advance.
EDIT:
I applied the technique in this answer, writing a class to remove the files after downloading. In my case, there are three files created. After one is selected the others get deleted but the selected file remains. I think this is because I am in Windows and there are restrictions on what Windows can do to open files. So, if that one file remains, I can live with that as it will get cleaned up the next time a user uses the site.