menpo.io.export_pickle(obj, fp, overwrite=False, protocol=2)[source]

Exports a given collection of Python objects with Pickle.

The fp argument can be either a Path or any Python type that acts like a file. If fp is a path, it must have the suffix .pkl or .pkl.gz. If .pkl, the object will be pickled using the selected Pickle protocol. If .pkl.gz the object will be pickled using the selected Pickle protocol with gzip compression (at a fixed compression level of 3).

Note that a special exception is made for pathlib.Path objects - they are pickled down as a pathlib.PurePath so that pickles can be easily moved between different platforms.

  • obj (object) – The object to export.
  • fp (Path or file-like object) – The string path or file-like object to save the object at/into.
  • overwrite (bool, optional) – Whether or not to overwrite a file if it already exists.
  • protocol (int, optional) –

    The Pickle protocol used to serialize the file. The protocols were introduced in different versions of python, thus it is recommended to save with the highest protocol version that your python distribution can support. The protocol refers to:

    Protocol Functionality
    0 Simplest protocol for text mode, backwards compatible.
    1 Protocol for binary mode, backwards compatible.
    2 Wider support for classes, compatible with python >= 2.3.
    3 Support for byte objects, compatible with python >= 3.0.
    4 Support for large objects, compatible with python >= 3.4.
  • ValueError – File already exists and overwrite != True
  • ValueErrorfp is a file-like object and extension is None
  • ValueError – The provided extension does not match to an existing exporter type (the output type is not supported).