2023.11.19使用flask制作一个文件夹生成器

2023.11.19使用flask制作一个文件夹生成器

实现功能:

(1)在指定路径上建立文件夹

(2)返回文件夹的路径和建立成功与否的提示

main.py

复制代码
import os
from flask import Flask, request, jsonify, render_template

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')


@app.route('/create_folder', methods=['POST'])
def create_folder():
    folder_name = request.json['folder_name'] # 获取前端传递的文件夹名称

    # 指定路径和文件夹名称
    base_path = 'static'  # 替换为实际的路径
    folder_path = os.path.join(base_path, folder_name)

    try:
        os.makedirs(folder_path)  # 在指定路径创建文件夹
        response = {
            'message': '文件夹创建成功',
            'folder_path': folder_path
        }
        return jsonify(response)
    except Exception as e:
        response = {
            'message': '文件夹创建失败',
            'error': str(e)
        }
        return jsonify(response), 500

if __name__ == '__main__':
    app.run(debug=True)

index.html

复制代码
<!DOCTYPE html>
<html>
<head>
    <title>创建文件夹</title>
</head>
<body>
    <h2>创建文件夹</h2>
    <input type="text" id="folderName" placeholder="请输入文件夹名称">
    <button onclick="createFolder()">创建</button>
    <p id="response"></p>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        function createFolder() {
            var folderName = $('#folderName').val();

            $.ajax({
                url: '/create_folder',
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({ 'folder_name': folderName }),
                success: function(response) {
                    $('#response').text(response.message + ',路径为:' + response.folder_path);
                },
                error: function(xhr, status, error) {
                    var errorMessage = xhr.responseText ? JSON.parse(xhr.responseText).message : '请求失败';
                    $('#response').text('错误:' + errorMessage);
                }
            });
        }
    </script>
</body>
</html>
相关推荐
zhiyouTech3 分钟前
从“被看见“到“被信任“:关于信源评级机制的一点观察
大数据·前端·人工智能
Hashan4 分钟前
Vibe Coding 下前后端怎么对接接口?后端不给力的兜底方案
前端·后端·vibecoding
李航19837 分钟前
自动动手开发图形引擎,不仅能AI建模,还能AI渲染
人工智能·python·计算机视觉·ai·ai编程
LeoCrawls12 分钟前
Python 读取 JSON 常见报错排查,附完整处理函数
python·json·php
小蒜学长12 分钟前
基于SpringBoot+Vue的小学数学智能出题系统(代码+数据库+LW)
java·数据库·spring boot·后端·智能出题系统
zzZ··*21 分钟前
CodeBuddy 用量看板:本地解析 Token 与积分消耗,不联网不上传
python·vue·ai编程
小猴子爱上树28 分钟前
跨马翻译:批量图片翻译+视频字幕+智能抠图一站式工具
python·音视频
小朱爱编程1231 小时前
我用 Jev 做了三个实用工具:整理标签页、分诊飞书反馈、找回 GitHub 收藏
java·开发语言·人工智能·后端·python·架构·ai编程
你挚爱的强哥1 小时前
【sgKeyboard】自定义组件:虚拟键盘
前端·javascript·计算机外设
锅里游的鱼吖1 小时前
echarts自定义折线图
前端·javascript·数据库