What the code down below does is accept a file and perform some operation. After that it generates some other file and send it as an attachment. After the file has been sent How could i delete that file.
from flask import Flask, render_template, request, redirect, url_for, send_file, session, flash
from additional import *
import os
import time
import json
app = Flask(__name__)
app.secret_key = " not 'soo secret' key"
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
form = request.form
file = request.files['file_name']
try:
splited_name = allowed_file(file.filename)
if splited_name[-1] == "txt":
file_contents = file.readlines()
file_contents = to_utf8(file_contents)
session['file_name'] = splited_name[0] + "." + str(time.time()) + '.txt'
if request.form['crypt'] == "encrypt":
with open("downloads/" + session['file_name'], "w") as f:
f.writelines(decrypted_text(file_contents))
if request.form['crypt'] == "decrypt":
with open("downloads/" + session['file_name'], "w") as f:
f.writelines(encrypted_text(file_contents))
return redirect(url_for('download_file'))
else:
flash("poindi ra mowa")
return redirect(url_for('index'))
except Exception:
return "exception occured"
return render_template('index.html')
@app.route('/download/file')
def download_file():
os.remove("downloads/" + session['file_name'])
return send_file("downloads/" + session['file_name'], as_attachment=True)
if __name__ == '__main__':
app.run(debug=True)
Aucun commentaire:
Enregistrer un commentaire