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>
相关推荐
张張4083 分钟前
(域格)环境搭建和编译
c语言·开发语言·python·ai
weixin_423533998 分钟前
【Windows11离线安装anaconda、python、vscode】
开发语言·vscode·python
一蓑烟雨,一任平生15 分钟前
鸿蒙H5调试方法
前端·华为·h5·harmonyos
武子康23 分钟前
大数据-263 实时数仓-Canal 增量订阅与消费原理:MySQL Binlog 数据同步实践
大数据·hadoop·后端
Canace28 分钟前
使用大模型来维护知识库
前端·人工智能
HashTang28 分钟前
用自然语言驱动的开源 3D 建筑设计编辑器-Aedifex
前端·github·ai编程
Ricky111zzz28 分钟前
leetcode学python记录1
python·算法·leetcode·职场和发展
程序猿_极客38 分钟前
SpringBoot 三大参数注解详解:@RequestParam @RequestBody @PathVariable 区别及常用开发注解
java·spring boot·后端·面试八股文·springboot注释
小白学大数据44 分钟前
Selenium+Python 爬虫:动态加载头条问答爬取
爬虫·python·selenium
T__TIII1 小时前
milvus 数据备份和还原
后端