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>
相关推荐
满怀冰雪2 分钟前
25-迁移学习入门:加载预训练模型并微调
人工智能·python·机器学习·迁移学习·paddle
CoderYanger3 分钟前
前端基础——JavaScript(WebAPI)代码案例
java·开发语言·前端·javascript·css·前端框架·html5
2601_962284503 分钟前
安卓APP UI自动化测试:Python + UiAutomator2 + pytest + pytest-html
python·pytest·uiautomator2·ui自动化测试·安卓app
geovindu5 分钟前
CSharp: Task Scheduler
开发语言·后端·c#·.net·.netcore
恋猫de小郭11 分钟前
Shopify 从 React Native 回到 Swift/Kotlin,但是你以为有手就行??
android·前端·ios
2601_9620780315 分钟前
构建RESTful APIs:使用Python和Flask
python·flask·web开发·api设计·构建restfulapis
半亩码田16 分钟前
C#转Python第4.5篇:单元测试:pytest vs xUnit/NUnit
python·单元测试·c#
2601_9623008121 分钟前
Python + Requests + Pytest + Excel + Allure:接口自动化测试项目实战
python·excel·pytest·allure·requests
Zane199423 分钟前
两个线程算两遍循环,为什么只快了一点点不是两倍?GIL连环追问
后端·python
重生之小比特23 分钟前
【Java SE】程序逻辑控制
java·开发语言·python