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

项目结构

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

复制代码
<!DOCTYPE html>
<html>
<head>
    <title>File Manager</title>
</head>
<body>
    <h1>File Manager</h1>
    <form method="POST">
        <input type="text" name="dirname" placeholder="Directory Name">
        <input type="submit" name="create" value="Create Directory">
        <input type="submit" name="delete" value="Delete Directory">
    </form>
    <ul>
        {% for directory in directories %}
            <li>{{ directory }}</li>
        {% endfor %}
    </ul>
</body>
</html>

效果图

相关推荐
weixin_421585011 天前
解释代码:val_pred = vxm_model.predict(val_input)--与tensor对比
python
xwill*1 天前
python 字符串拼接
linux·windows·python
superman超哥1 天前
仓颉锁竞争优化深度解析
c语言·开发语言·c++·python·仓颉
一晌小贪欢1 天前
【Python办公自动化】Python办公自动化常用库新手指南
开发语言·python·python自动化办公·python3·python办公自动化·python办公
其美杰布-富贵-李1 天前
Python 反射完整学习笔记
笔记·python·学习·反射
热心市民小刘05051 天前
12.23二叉树的层序遍历
python
yaoh.wang1 天前
力扣(LeetCode) 111: 二叉树的最小深度 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·深度优先
HealthScience1 天前
怎么搜索某个已知的药物的smiles
python
jimmyleeee1 天前
人工智能基础知识笔记二十八:几款有用的LLM管理工具
人工智能·笔记·python
schinber1 天前
Python包管理工具全景指南:从pip到现代化工具实战
python·conda·pip·uv