save_pickle¶
- pyhelpers.store.save_pickle(data, path_to_file, verbose=False, raise_error=False, **kwargs)[source]¶
Saves data to a pickle file.
- Parameters:
data (Any) – Data to be saved, compatible with the built-in pickle.dump() function.
path_to_file (str | os.PathLike) – Path where the Pickle file will be saved.
verbose (bool | int) – Whether to print relevant information to the console; defaults to
False
.raise_error (bool) – Whether to raise the provided exception; if
raise_error=False
(default), the error will be suppressed.kwargs – [Optional] Additional parameters for pickle.dump().
Examples:
>>> from pyhelpers.store import save_pickle >>> from pyhelpers.dirs import cd >>> from pyhelpers._cache import example_dataframe >>> pickle_dat = 1 >>> pickle_pathname = cd("tests", "data", "dat.pickle") >>> save_pickle(pickle_dat, pickle_pathname, verbose=True) Saving "dat.pickle" to ".\tests\data\" ... Done. >>> # Get an example dataframe >>> pickle_dat = example_dataframe() >>> pickle_dat Longitude Latitude City London -0.127647 51.507322 Birmingham -1.902691 52.479699 Manchester -2.245115 53.479489 Leeds -1.543794 53.797418 >>> save_pickle(pickle_dat, pickle_pathname, verbose=True) Updating "dat.pickle" at ".\tests\data\" ... Done.
Tip
The file path is validated before saving. Ensure the directory exists and is writable.
Supported compression formats:
.gz
(gzip),.xz
(LZMA compression) and.bz2
(bzip2).Other extensions are saved as uncompressed files.
Compression format is determined by the file extension. Ensure the extension matches the desired format.
See also
Examples for the function
load_pickle()
.