11

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

Saeed
  • 732
  • 1
  • 6
  • 19

3 Answers3

17

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.

mikazuki
  • 171
  • 3
7

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])))
Bob Smith
  • 26,929
  • 9
  • 72
  • 69
2

Uploading files and folders contain subfolders and files(images), Colab google:
Please, try this function to upload files and folders to Colab google:

from google.colab import files
import zipfile, io, os

    def read_dir_file(case_f):  # case_f = 0 for uploading one File and case_f = 1 for uploading one Zipped Directory
        uploaded = files.upload()    # to upload a Full Directory, please Zip it first (use WinZip)
        for fn in uploaded.keys():
            name = fn  #.encode('utf-8')
            #print('\nfile after encode', name)
            #name = io.BytesIO(uploaded[name])
        if case_f == 0:    # case of uploading 'One File only'
            print('\n file name: ', name)
            return name
        else:   # case of uploading a directory and its subdirectories and files
            zfile = zipfile.ZipFile(name, 'r')   # unzip the directory 
            zfile.extractall()
            for d in zfile.namelist():   # d = directory
                print('\n main directory name: ', d)
                return d
    print('Done!')

1- To upload One File:

fileName = read_dir_file(0)

If the file you are going to upload is a .csv file then:

import pandas as pd
df = pd.read_csv(fileName)
df.head()

You can read any file that has different formats by using the same manner.

2- To upload a Full Directory that has subdirectories and files: first zip the directory by using one zip and use:

dirName = read_dir_file(1)

Then, you can work with (dirName) as the root directory, as an example, if it has 3 subdirectories, say, (training, validation and test):

train_data_dir = dirName + 'training'  
validation_data_dir = dirName + 'validation'  
test_data_dir = dirName + 'test' 

That is it! Enjoy!

Yasser Mustafa
  • 111
  • 1
  • 5
  • did you try this on a directory of training images (say thousands for train, test, eval)? jpegs are already zipped, so the won't "zip" much and it seems that you have to do this every time you open the Colab notebook because the data is not persisted.. Colab does not seem to come with permanent "Disk". Super annoying, and for ML projects, particularly DL projects involving thousands or 10s of thousands of image, this is not usable... it takes hours to copy the data, and you have to do it every time you open the notebook. – Kai Apr 17 '18 at 19:09
  • Thx for your question, an I didn't try it to upload such big number of data but I agree with u that it will take very long time to be uploaded. Do you have any clue to solve that? thx. – Yasser Mustafa Apr 17 '18 at 20:33
  • no solution I know of. You have to copy the data every time you open the notebook. I think (I hope) the data may persist for 12 hours (that's the limit an using the virtual instance for Colab), but I haven't checked that. – Kai Apr 18 '18 at 19:34