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>
相关推荐
এ慕ོ冬℘゜6 分钟前
前端树形二级列表渲染 + 页面传参完整实战解析(附原生jQuery源码)
前端·javascript·jquery
ckjoker12 分钟前
我把Java多模态链路从0跑通了,结果先被4个坑狠狠干了一顿
后端·agent
事缓则圆17 分钟前
从零推导 Python 装饰器:不用背语法,看完这篇就懂了
python
鬼手点金20 分钟前
Hermes‑Agent 使用教程
python·ubuntu·powershell·cmd·ai agent·opencode·hermes
Gu Gu Study23 分钟前
【agent认知】宏观Agent的基本两层架构(Agent Loop与Agent Harness)
python·架构
从小就看凹凸曼^o^26 分钟前
Python基础3 - 流程控制语句:(1)程序结构
python
weixin_4692738135 分钟前
.md文件是什么?.md如何打开?
前端·编辑器
不好听61341 分钟前
Web Worker:浏览器给 JS 开的后台子线程
前端·浏览器
计算机魔术师1 小时前
数据分析还在靠人跑 SQL?AI 智能体已经把效率拉到 63 倍
前端
小陈的进阶之路1 小时前
爬虫三大解析库:lxml, jsonpath, bs4
开发语言·笔记·python