Skip to content

Instantly share code, notes, and snippets.

@thearn
Created April 20, 2013 00:57
Show Gist options
  • Save thearn/5424244 to your computer and use it in GitHub Desktop.
Save thearn/5424244 to your computer and use it in GitHub Desktop.
Functions for saving and loading python objects using pickle with gzip compression.
import pickle
import gzip
def save(object, filename, protocol = 0):
"""Saves a compressed object to disk
"""
file = gzip.GzipFile(filename, 'wb')
file.write(pickle.dumps(object, protocol))
file.close()
def load(filename):
"""Loads a compressed object from disk
"""
file = gzip.GzipFile(filename, 'rb')
buffer = ""
while True:
data = file.read()
if data == "":
break
buffer += data
object = pickle.loads(buffer)
file.close()
return object
@sonictl
Copy link

sonictl commented Dec 18, 2019

def load(filename):
        """Loads a compressed object from disk
        """
        file = gzip.GzipFile(filename, 'rb')
        # buffer = ""
        # while True:
        #         data = file.read()
        #         if data == "":
        #                 break
        #         buffer += data
        # object = pickle.loads(buffer)
        data = file.read()
        object = pickle.loads(data)
        file.close()
        return object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment