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>
相关推荐
BK贝壳4 小时前
bool-hybrid-array 的 GPU_opt 与 cjx:一个没显卡就报错的开关,一个把 800 字压到 96 字节的编码
python
考虑考虑8 小时前
tcpdump指令
运维·后端·自动化运维
BD_Marathon8 小时前
安装Jupyter notebook
ide·python·jupyter
Zlzxzw8 小时前
End-to-End Autonomous Generation of Human Assembly Plans
python
Wellijohn8 小时前
2000 个请求、30% 失败率:我把 Python 批量并发的限流和重试参数算了一遍
后端·python·性能优化
皮皮林5519 小时前
YouTube 是如何保存巨量视频文件的?
后端
学长毕设9 小时前
基于SpringBoot 车辆租赁系统平台-附源码
java·spring boot·后端
码途行者9 小时前
每天10分钟学Go:第22课 自定义错误
开发语言·后端·golang
feixing_fx10 小时前
解构与展开:可读性和性能的取舍
开发语言·前端·javascript·前端框架
Csvn10 小时前
Nginx 性能调优与压测:连接账本、找拐点与调优前后对比
后端·nginx