简体   繁体   中英

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).

I wanted to zip this folder and then pickle it. Say I have a zip file 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.

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

Short answer: You cannot pickle a Zip object.

Explanation: A Zip file is a file which has been compressed. The purpose of pickling is that we are trying to serialize some (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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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