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 分钟前
2026.5.6@霖宇博客制作中遇见的问题
前端·javascript·vue.js
QQ24221997913 分钟前
基于python+微信小程序的家教管理系统_mh3j9
开发语言·python·微信小程序
RSTJ_162543 分钟前
PYTHON+AI LLM DAY THREETY-SEVEN
开发语言·人工智能·python
郝学胜-神的一滴1 小时前
深度学习优化核心:梯度下降与网络训练全解析
数据结构·人工智能·python·深度学习·算法·机器学习
Aision_1 小时前
Agent 为什么需要 Checkpoint?
人工智能·python·gpt·langchain·prompt·aigc·agi
清水白石0081 小时前
《Python性能深潜:从对象分配开销到“小对象风暴”的破解之道(含实战与最佳实践)》
开发语言·python
小李子呢02111 小时前
前端八股Vue---Vue-router路由管理器
前端·javascript·vue.js
程序员飞哥1 小时前
重构 AI 思维(一):Prompt Engineering,如何下达不可违抗的指令?
人工智能·后端
Land03292 小时前
RPA工具选型技术指南:架构差异与实测数据
python·自动化·rpa
kafei_*2 小时前
VScode 添加 UV虚拟环境方法
vscode·python·uv