Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python file.save未以正确格式保存文件_Python_Flask - Fatal编程技术网

Python file.save未以正确格式保存文件

Python file.save未以正确格式保存文件,python,flask,Python,Flask,我必须在两个地方上传一个文件 在本地目录中 在Jira通过curl 我已经编写了postendpoint,它从请求中读取文件,并通过请求将相同的文件发送给Jira,在成功响应后,它将文件保存在本地 我的代码如下所示 for file in request.files.getlist('file'): filename = file.filename mimetype = file.content_type if not is_valid_type(mimetype

我必须在两个地方上传一个文件

  • 在本地目录中
  • 在Jira通过curl
  • 我已经编写了
    post
    endpoint,它从请求中读取文件,并通过请求将相同的文件发送给Jira,在成功响应后,它将文件保存在本地

    我的代码如下所示

     for file in request.files.getlist('file'):
         filename = file.filename
         mimetype = file.content_type
         if not is_valid_type(mimetype):
           return json.dumps({"success": False, "message": "Invalid File Format" }), 415
         files = {'file': (filename, file, mimetype)}
         r = requests.post(jira_url, files=files, headers=headers, auth=HTTPBasicAuth(current_app.config.get('username'), current_app.config.get('password')),verify=False)
         LOG.info("Got %s response from %s - text %s", r.status_code, "upload", r.json())
         data = r.json()
         filename = secure_filename(file.filename)
         file.save(os.path.join(current_app.config["UPLOAD_FOLDER"], filename))
    
    它保存了文件,但当我试图打开它时,它说我们不支持这种文件格式


    如果我从循环中删除对Jira的
    post
    调用,则它会以正确的格式保存文件。

    您是否尝试过打开/写入?:

    with open("input_file.txt", "w") as text_file:
        text_file.write("destination_file")
        text_file.close()
    
    指定路径目标的解决方案:

    import os
    filename = "input_file.txt"
    path = "/pathlocation/..."
    fullpath = os.path.join(path, filename)
    with open("input_file.txt", "w") as text_file:
        text_file.write(fullpath)
    text_file.close()
    

    post to jira调用不应影响files变量的内容