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>
相关推荐
IT_陈寒5 小时前
SpringBoot这个自动配置坑我跳了三次
前端·人工智能·后端
kyriewen6 小时前
我用 AI 一周写完了整个项目,上线第一天就崩了——这是我踩过最贵的 5 个坑
前端·javascript·ai编程
牧艺6 小时前
从零到协同:构建类飞书在线文档系统的五个技术重难点
前端·人工智能
用户395240998806 小时前
排坑日记:ASP.NET Core 中 "Required field is not provided" 验证错误全记录
后端
红尘散仙7 小时前
想写一个像样的终端 App?试试把 React 的开发体验搬进 Rust TUI
前端·rust
用户8356290780517 小时前
使用 Python 自动化 PowerPoint 形状布局与格式设置
后端·python
袋鼠云数栈UED团队7 小时前
一套 Spec-First 的 AI 编程工作流
前端·人工智能
袋鼠云数栈前端8 小时前
一套 Spec-First 的 AI 编程工作流
前端·ai+
angerdream8 小时前
Android手把手编写儿童手机远程监控App之vue3 路由守卫
前端
Oneslide8 小时前
sudo免密权限配置不生效
后端