-
-
Save zhangyuteng/902837c76f73264774bc326d157ea752 to your computer and use it in GitHub Desktop.
save/load compressed pickled objects in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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