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>
相关推荐
一点一木10 小时前
Seed Evolving 深度测评:我用它造了一个 AI 出题工具
人工智能·python·github
站大爷IP11 小时前
被 `asyncio.gather` 和 `wait` 坑惨了:异常处理的天壤之别
后端
张龙68711 小时前
Docker 镜像瘦身实战:从 1.2GB 到 128MB,我踩过的 7 个坑和一套可复制的方法论
运维·后端·docker
郭wes代码11 小时前
纯 CSS+JS 实战:手搓一张支持自定义的通用金色电子奖状(万字详细拆解)
前端·javascript·css
lizhongxuan11 小时前
工程技术:在智能体优先的世界中利用 Codex(转)
后端
阿童木写作11 小时前
跨境卖家效率翻倍,跨马翻译批量图片翻译工具实测
人工智能·python
鹿鹿学长11 小时前
2026国赛报名季:从组委会第一次通知到各校8月报名通知,赛程报名评奖官方口径一次讲清
python·自动化
C++、Java和Python的菜鸟12 小时前
第15章 项目部署(Docker)
python
阿黎梨梨12 小时前
React 表单处理与性能优化:从入门到进阶
前端·react.js
Whbbit199912 小时前
发布自己的 shadcn-vue 扩展组件,并支持 CLI 安装
前端·开源