Skip to content

Instantly share code, notes, and snippets.

@gauravssnl
Forked from pklaus/convert-pickle-version.py
Created December 14, 2016 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauravssnl/4baf323909ca60a5d262462e347f5150 to your computer and use it in GitHub Desktop.
Save gauravssnl/4baf323909ca60a5d262462e347f5150 to your computer and use it in GitHub Desktop.
A command line tool to convert between pickle protocol versions
#!/usr/bin/env python
import pickle
import argparse
import sys
def main():
parser = argparse.ArgumentParser(description='Convert between pickle protocol versions.')
parser.add_argument('input_file')
parser.add_argument('output_file')
parser.add_argument('--version', default=2, type=int, help='The pickle protocol version to write the OUTPUT_FILE.')
args = parser.parse_args()
with open(args.input_file, 'rb') as f:
try:
data = pickle.load(f)
except ValueError as e:
sys.stderr.write(str(e) + "\nPlease use a more recent version of Python to do the conversion.\n")
sys.exit(1)
with open(args.output_file, 'wb') as f:
pickle.dump(data, f, args.version)
print("Done!")
if __name__ == "__main__": main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment