项目结构
app.py
from flask import Flask, render_template, request, redirect, url_for
import os
import glob
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
to_delete = request.form.getlist('checks')
for file in to_delete:
os.remove(file)
return redirect(url_for('index'))
files = glob.glob('casefile/test*.py') # adjust this to your needs
return render_template('index.html', files=files)
if __name__ == '__main__':
app.run(debug=True)
templates / index.html
<!doctype html>
<html lang="en">
<head>
<title>File Deleter</title>
</head>
<body>
<h1>Select Files To Delete</h1>
<form method="POST">
{% for file in files %}
<input type="checkbox" name="checks" value="{{ file }}"> {{ file }}<br />
{% endfor %}
<input type="submit" value="Delete">
</form>
</body>
</html>
示例图