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=format._MAX_HEADER_SIZE)
| 281 | |
| 282 | @set_module('numpy') |
| 283 | def load(file, mmap_mode=None, allow_pickle=False, fix_imports=True, |
| 284 | encoding='ASCII', *, max_header_size=format._MAX_HEADER_SIZE): |
| 285 | """ |
| 286 | Load arrays or pickled objects from ``.npy``, ``.npz`` or pickled files. |
| 287 | |
| 288 | .. warning:: Loading files that contain object arrays uses the ``pickle`` |
| 289 | module, which is not secure against erroneous or maliciously |
| 290 | constructed data. Consider passing ``allow_pickle=False`` to |
| 291 | load data that is known not to contain object arrays for the |
| 292 | safer handling of untrusted sources. |
| 293 | |
| 294 | Parameters |
| 295 | ---------- |
| 296 | file : file-like object, string, or pathlib.Path |
| 297 | The file to read. File-like objects must support the |
| 298 | ``seek()`` and ``read()`` methods and must always |
| 299 | be opened in binary mode. Pickled files require that the |
| 300 | file-like object support the ``readline()`` method as well. |
| 301 | mmap_mode : {None, 'r+', 'r', 'w+', 'c'}, optional |
| 302 | If not None, then memory-map the file, using the given mode (see |
| 303 | `numpy.memmap` for a detailed description of the modes). A |
| 304 | memory-mapped array is kept on disk. However, it can be accessed |
| 305 | and sliced like any ndarray. Memory mapping is especially useful |
| 306 | for accessing small fragments of large files without reading the |
| 307 | entire file into memory. |
| 308 | allow_pickle : bool, optional |
| 309 | Allow loading pickled object arrays stored in npy files. Reasons for |
| 310 | disallowing pickles include security, as loading pickled data can |
| 311 | execute arbitrary code. If pickles are disallowed, loading object |
| 312 | arrays will fail. Default: False |
| 313 | |
| 314 | .. versionchanged:: 1.16.3 |
| 315 | Made default False in response to CVE-2019-6446. |
| 316 | |
| 317 | fix_imports : bool, optional |
| 318 | Only useful when loading Python 2 generated pickled files on Python 3, |
| 319 | which includes npy/npz files containing object arrays. If `fix_imports` |
| 320 | is True, pickle will try to map the old Python 2 names to the new names |
| 321 | used in Python 3. |
| 322 | encoding : str, optional |
| 323 | What encoding to use when reading Python 2 strings. Only useful when |
| 324 | loading Python 2 generated pickled files in Python 3, which includes |
| 325 | npy/npz files containing object arrays. Values other than 'latin1', |
| 326 | 'ASCII', and 'bytes' are not allowed, as they can corrupt numerical |
| 327 | data. Default: 'ASCII' |
| 328 | max_header_size : int, optional |
| 329 | Maximum allowed size of the header. Large headers may not be safe |
| 330 | to load securely and thus require explicitly passing a larger value. |
| 331 | See :py:func:`ast.literal_eval()` for details. |
| 332 | This option is ignored when `allow_pickle` is passed. In that case |
| 333 | the file is by definition trusted and the limit is unnecessary. |
| 334 | |
| 335 | Returns |
| 336 | ------- |
| 337 | result : array, tuple, dict, etc. |
| 338 | Data stored in the file. For ``.npz`` files, the returned instance |
| 339 | of NpzFile class must be closed to avoid leaking file descriptors. |
| 340 |