How to import and read a shelve or Numpy file in Google Colaboratory?

Viewed 15239

I have file.npy and I want to load it in Google Colaboratory Notebook. I already know that I must load the file from Google Drive, however I have no idea how to do so.

Any help is welcome

3 Answers

Upload your file into Colaboratory Notebook with the following:

from google.colab import files
uploaded = files.upload()

Then you can reach the contents of your file from uploaded object and then write it into a file:

with open("my_data.h5", 'w') as f:
    f.write(uploaded[uploaded.keys()[0]])

If you run:

!ls

you will see my_data.h5 file in the current directory.

This is the method that worked for me. Hope it helps.

Actually, you can directly upload and download local files.

There are examples of local file upload/download as well as Drive file upload / download in the I/O example notebook

The first cell shows local file upload:

from google.colab import files
uploaded = files.upload()
for fn in uploaded.keys():
  print('User uploaded file "{name}" with length {length} bytes'.format(
      name=fn, length=len(uploaded[fn])))
Related