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>
相关推荐
博傅4 小时前
Spring 核心原理
java·后端·spring
用户938515635075 小时前
掘金风格后端数据库设计实战:从用户表到分布式架构,一文搞定
后端·sql
拾光师5 小时前
Hadoop 三种运行模式:从单机调试到生产集群,一路搭过来
后端
leisoo80975 小时前
ig50数据落盘ClickHousevsTimescaleDBvsDuckDB实测对比 IG50免费开源股票数据API接口
开发语言·jvm·数据库·python·json
进阶的小名6 小时前
Spring AI 2.0 探索:多 OpenAI-Compatible 模型接入,以及下一代 Session 记忆管理
java·人工智能·后端·gpt·spring·ai·chatgpt
卷无止境6 小时前
FastAPI 的 Metadata 到底是什么,又牵动了哪些核心概念
后端·python
卷无止境6 小时前
FastAPI 调试实战,从断点到生产环境的排错心法
后端·python
敢敢のwings6 小时前
智元 GO-2 与 AgiBot-World 深度解读
开发语言·后端·golang