Load local data files to Colaboratory

Viewed 141404

I just wondering that is it possible to load local data files(like .xlsx or .csv files that on my google drive) into Colaboratory?

9 Answers

I was a bit confused by the example for loading local files on first glance as there was no place to specify a file path. All you need to do is copy and paste the recipe to figure this out, but to be clear:

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

will open an upload dialogue window where you can browse and select your local files for upload.

Then

for fn in uploaded.keys():
  print('User uploaded file "{name}" with length {length} bytes'.format(
      name=fn, length=len(uploaded[fn])))

will show you the keys to access what you just uploaded.

Edit for additional clarification: The dictionary uploaded will have keys of the selected filenames - so if for example you select a file my_test.txt, then you would access that file using uploaded['my_test.txt'].

Yes, all of these scenarios are supported.

For recipes to access local and Drive files, check out the I/O example notebook.

For access to xls files, you'll want to upload the file to Google Sheets. Then, you can use the gspread recipes in the same I/O example notebook.

A recently added way to upload local files is to use the 'Files' tab in the right hand side drawer.

enter image description here

From there, you can upload a local file using the 'upload' button.

enter image description here

(You can also download files by right clicking on them in the file tree.)

To load local data files to Colab:

Method 1: Google Drive Method

  1. Upload data file from system memory to Google drive.
  2. Mount Google drive in Colab

    from google.colab import drive drive.mount('/content/gdrive')

  3. Then-> path = "/gdrive/My Drive/filename"

You can now access google drive files in Google Colab.

Method 2: Direct Load

from google.colab import files
def getLocalFiles():
    _files = files.upload()
    if len(_files) >0:
       for k,v in _files.items():
         open(k,'wb').write(v)
getLocalFiles()

Method 3: Using import files

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

It's a 2 step process.

Step 1 : First invoke a file selector with in your colab notebook with the following code

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

this will take you to a file browser window

step 2 : To load the content of the file into Pandas dataframe, use the following code

import pandas as pd
import io
df = pd.read_csv(io.StringIO(uploaded['iris.csv'].decode('utf-8')))
print(df)

To get data from your system to colab try this:

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

Choose the file you want to upload and hit enter and its done. For example, I have uploaded an image and displayed it using the code below:

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('image.jpg')
img_cvt = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

plt.imshow(img_cvt)
plt.show()

You can use this URL for uploading your files in Google Colab:

https://colab.research.google.com/notebooks/io.ipynb#scrollTo=vz-jH8T_Uk2c

go to Local file system>Downloading files to your local file system Then run the code. After that, browser button will be appeared for you to uploading your files from your PC.

Related