Flask 创建文件目录,删除文件目录

 项目结构

Flask 创建文件目录,删除文件目录_第1张图片

 app.py

from flask import Flask, render_template, request, redirect, url_for
import os

app = Flask(__name__)
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
FILE_DIR = os.path.join(BASE_DIR, 'testfile')


@app.route('/', methods=['GET', 'POST'])
def index():

    if request.method == 'POST':
        if 'create' in request.form:
            dirname = request.form.get('dirname')
            os.mkdir(os.path.join(FILE_DIR, dirname))
        elif 'delete' in request.form:
            dirname = request.form.get('dirname')
            os.rmdir(os.path.join(FILE_DIR, dirname))
        return redirect(url_for('index'))

    directories = os.listdir(FILE_DIR)
    return render_template('index.html', directories=directories)

if __name__ == '__main__':
    if not os.path.isdir('testfile'):
        os.makedirs('testfile')
    app.run(debug=True)

templates / index.html




    File Manager


    

File Manager

    {% for directory in directories %}
  • {{ directory }}
  • {% endfor %}

效果图

Flask 创建文件目录,删除文件目录_第2张图片

 

你可能感兴趣的:(#,Flask,flask,python)