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>
相关推荐
阿童木写作2 小时前
跨境电商图片翻译工具推荐:批量图片翻译+视频字幕翻译+智能抠图
python·macos·音视频·xcode
cui_ruicheng3 小时前
LangChain 应用开发(十四):Agent 上下文与记忆机制
服务器·人工智能·python·langchain
平头哥~3 小时前
Day 07 _ 那条 1px 的线,为什么在手机上忽粗忽细
前端·css·样式·学习资料
小江的记录本3 小时前
【CSS】CSS 动画:transition、animation、transform、CSS3 新特性(附《思维导图》)
前端·css·安全·spring·前端框架·css3·动画
circuitsosk3 小时前
任务规划器的三种范式对比:ReAct、Plan-and-Execute 与 Tree-of-Thought 在真实业务中的取舍
前端·javascript·python·react.js·llm·ai agent
shehuiyuelaiyuehao3 小时前
算法31,前缀和,可被k整除的子数组
数据结构·python·算法
宸津-代码粉碎机4 小时前
AI攻防战升级!基于Spring AI构建Java应用自动免疫安全体系
java·大数据·开发语言·人工智能·python·安全·spring
测试19984 小时前
如何用appium搭建Android自动化测试框架?
自动化测试·软件测试·python·测试工具·职场和发展·appium·测试用例
程序员大雄学编程4 小时前
微积分46. 无穷级数四大核心性质:从理论到实践的全方位解析
python·学习·微积分·学习工具
hzxpaipai4 小时前
企业官网技术架构拆解:前端、后台、数据库、服务器如何协同
前端·数据库·架构