小编gab*_*aba的帖子

Python创建zip文件

我使用以下脚本来创建ZIP文件:

import zipfile
import os

def zip_file_generator(filenames, size):

    # filenames = path to the files

    zip_subdir = "SubDirName"
    zip_filename = "SomeName.zip"

    # Open BytesIO to grab in-memory ZIP contents
    s = io.BytesIO()

    # The zip compressor
    zf = zipfile.ZipFile(s, "w")

    for fpath in filenames:
        # Calculate path for file in zip
        fdir, fname = os.path.split(fpath)
        zip_path = os.path.join(zip_subdir, fname)

        # Add file, at correct path
        zf.write(fpath, zip_path)

    # Must close zip for all contents to be written
    zf.close()

    # Grab …
Run Code Online (Sandbox Code Playgroud)

python django python-3.x

9
推荐指数
1
解决办法
1164
查看次数

标签 统计

django ×1

python ×1

python-3.x ×1