Skip to content

Instantly share code, notes, and snippets.

@zhangyuteng
Forked from cwidmer/compressed_pickle.py
Created March 29, 2018 12:13
Show Gist options
  • Save zhangyuteng/902837c76f73264774bc326d157ea752 to your computer and use it in GitHub Desktop.
Save zhangyuteng/902837c76f73264774bc326d157ea752 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment