Pickle / Save Virtual File in Python to Disk

125 Views Asked by At

I would like to pickle a virtual file within a dictionary.

Specifically, I have a function G with a method save() that takes a file-obj or a string (filename) to save it's content to disk.

But I would like to save it to a virtual file first, then put this virtual file into a dict together with associate data and then save (pickle) the complete dictionary to disk.

How can I save to an in-memory file, include it in a dictionary and pickle that? Or how would you do it?

Something like:

out = {
    'var1': var1,
    'var2': var2
    }

with tempfile.SpooledTemporaryFile() as file:

    G.save(file)
    out['G'] = file

Thanks a lot!

1

There are 1 best solutions below

1
Marc On BEST ANSWER

I think what you want to do should be possible by the following:

out = {
    'var1': var1,
    'var2': var2
    }

with tempfile.SpooledTemporaryFile() as file:
    G.save(file)
    file.seek(0) # reset the file handle to the start
    out['G'] = file.read() # read the binary data into your dict

The binary stream can later be converted back to a file-like obj. using io.BytesIO( out['G'] ).