简体   繁体   English

我可以腌制 Zip object 吗?

[英]Can I pickle a Zip object?

I have a directory containing mostly text and json files, and one binary file (output of MXNet.Block.save_parameters neural network).我的目录主要包含文本和 json 文件,以及一个二进制文件( MXNet.Block.save_parameters神经网络的输出)。

I wanted to zip this folder and then pickle it.我想 zip 这个文件夹然后腌制它。 Say I have a zip file object:假设我有一个 zip 文件 object:

from zipfile import ZipFile
import os, pickle, itertools


files = list(itertools.chain(*[
    map(lambda x: os.path.join(root, x), files)
    for root, directories, files in os.walk('model-artifacts/')
]))

zfile = ZipFile('mymode.l.zip', 'w')

for file in file_paths:
   zfile.write(file)

I cannot really pickle it:我真的不能腌制它:

pickle.dumps(zfile)

# TypeError: cannot serialize '_io.BufferedRandom' object

I was wondering if there is a way to pickle zipfiles or any way to pickle contents of a directory.我想知道是否有办法腌制 zipfile 或任何腌制目录内容的方法。

WHY?为什么?

I am not doing the pickling myself, but using a library Metaflow which pickles objects in it, so I want to find a way to store my model with Metaflow我不是自己做酸洗,而是使用一个库Metaflow来酸洗其中的对象,所以我想找到一种方法来用 Metaflow 存储我的Metaflow

Short answer: You cannot pickle a Zip object.简短回答:您不能腌制 Zip object。

Explanation: A Zip file is a file which has been compressed.说明: Zip 文件是已压缩的文件。 The purpose of pickling is that we are trying to serialize some (python) object.酸洗的目的是我们试图序列化一些(python)object。 However, after file compression, you don't really have (python) objects anymore, simply a bunch of 0's and 1's ready to be decompressed.但是,在文件压缩之后,您不再真正拥有 (python) 对象,只是一堆 0 和 1 准备解压缩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM