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>
相关推荐
蓝创工坊Blue Foundry14 分钟前
扫描件批量转 Excel:先确认要整表还原还是字段汇总
python·pdf·ocr·excel
IT_陈寒18 分钟前
Python的线程池把我CPU跑满了,原来少传了个参数
前端·人工智能·后端
二十雨辰18 分钟前
[爬虫]-解析
开发语言·python
喜欢的名字被抢了26 分钟前
写-Chrome-扩展时踩过的那些-MV3-的坑
前端·chrome
网安老伯36 分钟前
网络安全基础要点知识介绍(非常详细),零基础入门到精通,看这一篇就够了
运维·前端·网络协议·web安全·网络安全·职场和发展
wuqingshun31415941 分钟前
springBoot是如何通过main方法启动web项目的?
前端·spring boot·后端
xxwl58542 分钟前
HTML 小总结:从骨架到枝叶,系统掌握网页结构
前端·html
●VON43 分钟前
鸿蒙 PC Markdown 编辑器 Bridge 协议:原生外壳与 Web 内核的状态同步
前端·华为·编辑器·harmonyos·鸿蒙
MrDJun1 小时前
分享一款免费实用的网页变动监控工具:PageWatch.tech
前端·javascript·vue.js
天天爱吃肉82181 小时前
AI Agent时代你的经验值多少钱?
人工智能·python·功能测试·学习·汽车