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>

效果图

相关推荐
Lightning-py25 分钟前
ASCII,十进制,十六进制,八进制和二进制转换表
python
laplace01231 小时前
deque+yield+next语法
人工智能·笔记·python·agent·rag
福大大架构师每日一题1 小时前
2026-01-15:下一个特殊回文数。用go语言,给定一个整数 n,求出一个比 n 更大的最小整数,该整数需要满足两条规则: 1. 它的十进制表示从左到右与从右到左完全一致(即读起来是对称的)。 2
python·算法·golang
芝士爱知识a2 小时前
[2026深度测评] AI期权交易平台推荐榜单:AlphaGBM领跑,量化交易的新范式
开发语言·数据结构·人工智能·python·alphagbm·ai期权工具
overmind2 小时前
oeasy Python 113 内置函数sorted中使用 reverse和key
开发语言·python
AC赳赳老秦2 小时前
2026主权AI趋势:DeepSeek搭建企业自有可控AI环境,保障数据安全实战
大数据·数据库·人工智能·python·科技·rabbitmq·deepseek
小小张说故事2 小时前
OpenCV Python技术文档
python·opencv
PD我是你的真爱粉2 小时前
Redis持久化、内存管理、慢查询与发布订阅
redis·python·mybatis
查无此人byebye3 小时前
实战DDPM扩散模型:MNIST手写数字生成+FID分数计算(完整可运行版)
人工智能·pytorch·python·深度学习·音视频
好家伙VCC3 小时前
# 光计算驱动的编程范式革新:用Python实现光子神经网络模拟器在传统电子计算架构逼近物理极限的今天,**光计算**正
java·开发语言·python·神经网络