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>
相关推荐
梦帮科技8 分钟前
第三十四篇:开源社区运营:GitHub Stars增长策略
开发语言·前端·爬虫·python·docker·架构·html
time_rg9 分钟前
react fiber与事件循环
前端·react.js
liu****11 分钟前
机器学习-线性回归
人工智能·python·算法·机器学习·回归·线性回归
Mr_chiu17 分钟前
告别“代码屎山”:用Cursor系统重构遗留前端项目
前端·cursor
LC同学4798125 分钟前
工程化实战:uniapp基于路由的自动主题切换体系
前端
莫比乌斯环30 分钟前
【安全专项】如何成为一名“火眼金睛”的安卓侦探?
前端·代码规范
LC同学4798130 分钟前
深入解析:uniapp单仓库多应用(SaaS 化)架构
前端
阿蔹33 分钟前
Python-Pytest
python·自动化·pytest
苗苗大佬38 分钟前
session和cookies
python
开心猴爷40 分钟前
Perfdog 成本变高之后,Windows 上还能怎么做 iOS APP 性能测试
后端