Loading .npz in colab using mounted drive

Viewed 1691

How do I load an .npz file that has been mounted from google drive in a google colab notebook?

Given the following code

from google.colab import drive
import numpy as np
drive.mount('/content/drive')
data = np.load('/content/drive/My Drive/Colab Notebooks/project/data/dat.npz')

I get this error

ValueError                                Traceback (most recent call last)
<ipython-input-40-168dd0fea229> in <module>()
      1 data={}
      2 for path in glob('/content/drive/My Drive/Colab Notebooks/lol/data/unzipped/*'):
----> 3   data[os.path.basename(os.path.splitext(path)[0])]=np.load(path)

/usr/local/lib/python3.6/dist-packages/numpy/lib/npyio.py in load(file, mmap_mode, allow_pickle, fix_imports, encoding)
    455             # Try a pickle
    456             if not allow_pickle:
--> 457                 raise ValueError("Cannot load file containing pickled data "
    458                                  "when allow_pickle=False")
    459             try:

ValueError: Cannot load file containing pickled data when allow_pickle=False

I have tried setting allow_pickle=True, but that results in this error

---------------------------------------------------------------------------
EOFError                                  Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/numpy/lib/npyio.py in load(file, mmap_mode, allow_pickle, fix_imports, encoding)
    459             try:
--> 460                 return pickle.load(fid, **pickle_kwargs)
    461             except Exception:

EOFError: Ran out of input

During handling of the above exception, another exception occurred:

OSError                                   Traceback (most recent call last)
1 frames
/usr/local/lib/python3.6/dist-packages/numpy/lib/npyio.py in load(file, mmap_mode, allow_pickle, fix_imports, encoding)
    461             except Exception:
    462                 raise IOError(
--> 463                     "Failed to interpret file %s as a pickle" % repr(file))
    464     finally:
    465         if own_fid:

OSError: Failed to interpret file '/content/drive/My Drive/Colab Notebooks/lol/data/unzipped/euk_test.npz' as a pickle

Python version: 3.6.9

Numpy version: 1.18.2

1 Answers
from scipy.sparse import load_npz
data = load_npz('/content/drive/My Drive/ColabNotebooks/project/data/dat.npz')

Documentation

Related