2023.11.23使用flask实现在指定路径生成文件夹操作

2023.11.23使用flask实现在指定路径生成文件夹操作

程序比较简单,实现功能:

1、前端输入文件夹

2、后端在指定路径生成文件夹

3、前端反馈文件夹生成状态

main.py

复制代码
from flask import Flask, request, render_template
import os

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/create_folder', methods=['POST'])
def create_folder():
    folder_name = request.form['folderName']  # 从前端获取文件夹名称
    base_path = 'static/'  # 指定路径

    folder_path = os.path.join(base_path, folder_name)

    try:
        os.makedirs(folder_path, exist_ok=True)  # 创建文件夹
        message = f'Folder "{folder_name}" created successfully at path: {folder_path}'
    except Exception as e:
        message = f'Error creating folder: {str(e)}'

    return render_template('index.html', message=message)


if __name__ == '__main__':
    app.run()

index.html

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Create Folder</title>
</head>
<body>
    <form action="/create_folder" method="post">
        <input type="text" name="folderName" placeholder="Enter folder name">
        <button type="submit">Create Folder</button>
    </form>

    <div id="message">
        {% if message %}
            <p>{{ message }}</p>
        {% endif %}
    </div>
</body>
</html>
相关推荐
全栈项目管理程序猿2 分钟前
ArcGIS JS 基础教程(28):图层渲染顺序管理
前端·javascript
ZOnePieceC2 分钟前
消息队列之Kafka
后端
yunwei372 分钟前
eBPF 入门实践教程十七:编写 eBPF 程序统计随机/顺序磁盘 I/O
linux·后端·性能优化
花间相见3 分钟前
【计算基础|网络07】HTTPS(下):ECDHE 握手与优化
后端
QuantiCore_IO4 分钟前
从请求风暴到可维护的数据管道:量化系统为什么需要批量接口?
后端·github·api
136096757235 分钟前
.env 的三个必查项
后端
长安米粒贵5 分钟前
接口超时了,为什么重试反而把系统打垮?聊聊超时预算的 4 个误区
后端
huakoh5 分钟前
MCP 工具报错走哪条通道:三条探针的最小复现检查
前端
程序员Sunday12 分钟前
Spring @Transactional 没回滚?按代理调用、异常和传播行为排查
java·后端·spring
东莞市云毅网络有限公司12 分钟前
结构化标注缺失怎么查:JSON-LD抽取与每日巡检脚本实现
python·sqlite·自动化运维·geo·数据监测