Skip to content

Instantly share code, notes, and snippets.

@cwidmer
Last active August 22, 2021 06:19
Show Gist options
  • Save cwidmer/7813392 to your computer and use it in GitHub Desktop.
Save cwidmer/7813392 to your computer and use it in GitHub Desktop.
save/load compressed pickled objects in python
import cPickle
import bz2
def save(filename, myobj):
"""
save object to file using pickle
@param filename: name of destination file
@type filename: str
@param myobj: object to save (has to be pickleable)
@type myobj: obj
"""
try:
f = bz2.BZ2File(filename, 'wb')
except IOError, details:
sys.stderr.write('File ' + filename + ' cannot be written\n')
sys.stderr.write(details)
return
cPickle.dump(myobj, f, protocol=2)
f.close()
def load(filename):
"""
Load from filename using pickle
@param filename: name of file to load from
@type filename: str
"""
try:
f = bz2.BZ2File(filename, 'rb')
except IOError, details:
sys.stderr.write('File ' + filename + ' cannot be read\n')
sys.stderr.write(details)
return
myobj = cPickle.load(f)
f.close()
return myobj
@UdayaPrakash98
Copy link

Can we use decompress in flask app to load model ?

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