Flask_Tool上传下载压缩文件

  • 上传文件
@mod.route('/upload', methods=['POST', 'GET'])
def upload():
    if request.method == 'POST':
        f = request.files['file']
        basepath = r"D:\exe_file"
        upload_path = os.path.join(basepath, 'testexe', secure_filename(f.filename))  # 注意:没有的文件夹一定要先创建,不然会提示没有该路径
        f.save(upload_path)
  • 下载文件
@mod.route("/download", methods=["GET"])
def download():
    exe_record_id = request.args.get("exe_record_id", 1)
    pre_path = r"D:\exe_file_record\%s" % exe_record_id
    if not os.path.exists(pre_path + '\\' + str(exe_record_id) + ".zip"):
        return jsonify({"msg": "Please wait while the report is being generated.", "ret": -1})
    response = make_response(
        send_from_directory(pre_path, str(exe_record_id) + ".zip", as_attachment=True))
    return response
  • 压缩文件
def zip_file(f1, f2, report_id):
    # f1, f2为文件路径
    f = zipfile.ZipFile(f1 + "\\" + str(report_id) + ".zip", 'w', zipfile.ZIP_DEFLATED)
    dirpath = f2
    for path, dirnames, filenames in os.walk(dirpath):
        # 去掉目标根路径,只对目标文件夹下边的文件及文件夹进行压缩
        fpath = path.replace(dirpath, '')
        for filename in filenames:
            f.write(os.path.join(path, filename), os.path.join(fpath, filename))
        f.close()
  • 执行exe文件
command = "D:\\channel_trunk\\bin64\\your_exe.exe %s %s %s %s" % (base_client, source_file_list,
                                                                                 product_dir, parm_shd)
    # KJx3ResStoreMoveMgr.exe <客户路径>  <清单文件>  <输出报告路径>  <0|1>
    print(command)
    # os.system(command)
    f = os.popen(command)
    data = f.readlines()
    f.close()
    print(data)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Loganer

感谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值