Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 333
Saeed Avatar asked Nov 09 '17 22:11

Saeed


People also ask

How do I upload and read files in google Colab?

Click on “Choose Files” then select and upload the file. Wait for the file to be 100% uploaded. You should see the name of the file once Colab has uploaded it. Finally, type in the following code to import it into a dataframe (make sure the filename matches the name of the uploaded file).


2 Answers

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])))
like image 140
Bob Smith Avatar answered Sep 22 '22 09:09

Bob Smith


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!

like image 39
Yasser M Avatar answered Sep 18 '22 09:09

Yasser M