相关疑难解决方法(0)

zip文件并避免目录结构

我有一个拉链文件(new.txt)的Python脚本.

tofile =  "/root/files/result/"+file
targetzipfile = new.zip   # This is how I want my zip to look like
zf = zipfile.ZipFile(targetzipfile, mode='w')
try:
    #adding to archive
    zf.write(tofile)
finally:
    zf.close()
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我得到了zip文件.但是当我尝试解压缩文件时,我得到的文件文件位于与文件路径对应的一系列目录中,即我看到目录root中有一个文件夹result,里面有更多目录,即我有

/root/files/result/new.zip

当我解压缩时,new.zip我有一个看起来像的目录结构

/root/files/result/root/files/result/new.txt.
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以拉链,这样当我解压缩时我才会得到new.txt.

换句话说,我有/root/files/result/new.zip,当我解压缩时new.zip,它应该看起来像

/root/files/results/new.txt
Run Code Online (Sandbox Code Playgroud)

python zip

21
推荐指数
7
解决办法
2万
查看次数

Flask:如何向客户端发送动态生成的zipfile

我正在寻找一种方法将zipfile发送到客户端,该方法是从请求响应生成的.在此示例中,我将一个JSON字符串发送到url,该url返回已转换的JSON字符串的zip文件.

@app.route('/sendZip', methods=['POST'])
def sendZip():
    content = '{"type": "Point", "coordinates": [-105.01621, 39.57422]}'
    data = {'json' : content}
    r = requests.post('http://ogre.adc4gis.com/convertJson', data = data)
    if r.status_code == 200:
        zipDoc = zipfile.ZipFile(io.BytesIO(r.content))
        return Response(zipDoc,
                mimetype='application/zip',
                headers={'Content-Disposition':'attachment;filename=zones.zip'})
Run Code Online (Sandbox Code Playgroud)

但是我的zip文件是空的,而烧瓶返回的错误是

Debugging middleware caught exception in streamed response at a point where response 
headers were already sent
Run Code Online (Sandbox Code Playgroud)

python json zipfile flask

7
推荐指数
1
解决办法
6586
查看次数

标签 统计

python ×2

flask ×1

json ×1

zip ×1

zipfile ×1