Load arrays or pickled objects from ``.npy``, ``.npz`` or pickled files. .. warning:: Loading files that contain object arrays uses the ``pickle`` module, which is not secure against erroneous or maliciously constructed data. Consider passing ``allow_pickl
(file, mmap_mode=None, allow_pickle=False, fix_imports=True,
encoding='ASCII', *, max_header_size=_MAX_HEADER_SIZE)
| 310 | |
| 311 | @set_module('numpy') |
| 312 | def load(file, mmap_mode=None, allow_pickle=False, fix_imports=True, |
| 313 | encoding='ASCII', *, max_header_size=_MAX_HEADER_SIZE): |
| 314 | """ |
| 315 | Load arrays or pickled objects from ``.npy``, ``.npz`` or pickled files. |
| 316 | |
| 317 | .. warning:: Loading files that contain object arrays uses the ``pickle`` |
| 318 | module, which is not secure against erroneous or maliciously |
| 319 | constructed data. Consider passing ``allow_pickle=False`` to |
| 320 | load data that is known not to contain object arrays for the |
| 321 | safer handling of untrusted sources. |
| 322 | |
| 323 | Parameters |
| 324 | ---------- |
| 325 | file : file-like object, string, or pathlib.Path |
| 326 | The file to read. File-like objects must support the |
| 327 | ``seek()`` and ``read()`` methods and must always |
| 328 | be opened in binary mode. Pickled files require that the |
| 329 | file-like object support the ``readline()`` method as well. |
| 330 | mmap_mode : {None, 'r+', 'r', 'w+', 'c'}, optional |
| 331 | If not None, then memory-map the file, using the given mode (see |
| 332 | `numpy.memmap` for a detailed description of the modes). A |
| 333 | memory-mapped array is kept on disk. However, it can be accessed |
| 334 | and sliced like any ndarray. Memory mapping is especially useful |
| 335 | for accessing small fragments of large files without reading the |
| 336 | entire file into memory. |
| 337 | allow_pickle : bool, optional |
| 338 | Allow loading pickled object arrays stored in npy files. Reasons for |
| 339 | disallowing pickles include security, as loading pickled data can |
| 340 | execute arbitrary code. If pickles are disallowed, loading object |
| 341 | arrays will fail. Default: False |
| 342 | fix_imports : bool, optional |
| 343 | Only useful when loading Python 2 generated pickled files, |
| 344 | which includes npy/npz files containing object arrays. If `fix_imports` |
| 345 | is True, pickle will try to map the old Python 2 names to the new names |
| 346 | used in Python 3. |
| 347 | encoding : str, optional |
| 348 | What encoding to use when reading Python 2 strings. Only useful when |
| 349 | loading Python 2 generated pickled files, which includes |
| 350 | npy/npz files containing object arrays. Values other than 'latin1', |
| 351 | 'ASCII', and 'bytes' are not allowed, as they can corrupt numerical |
| 352 | data. Default: 'ASCII' |
| 353 | max_header_size : int, optional |
| 354 | Maximum allowed size of the header. Large headers may not be safe |
| 355 | to load securely and thus require explicitly passing a larger value. |
| 356 | See :py:func:`ast.literal_eval()` for details. |
| 357 | This option is ignored when `allow_pickle` is passed. In that case |
| 358 | the file is by definition trusted and the limit is unnecessary. |
| 359 | |
| 360 | Returns |
| 361 | ------- |
| 362 | result : array, tuple, dict, etc. |
| 363 | Data stored in the file. For ``.npz`` files, the returned instance |
| 364 | of NpzFile class must be closed to avoid leaking file descriptors. |
| 365 | |
| 366 | Raises |
| 367 | ------ |
| 368 | OSError |
| 369 | If the input file does not exist or cannot be read. |