Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import local file to google colab

I don't understand how colab works with directories, I created a notebook, and colab put it in /Google Drive/Colab Notebooks.

Now I need to import a file (data.py) where I have a bunch of functions I need. Intuition tells me to put the file in that same directory and import it with:

import data

but apparently that's not the way...

I also tried adding the directory to the set of paths but I am specifying the directory incorrectly..

Can anyone help with this?

Thanks in advance!

like image 983
Danf Avatar asked Mar 21 '18 23:03

Danf


People also ask

Can Google colab use local file?

Colaboratory lets you connect to a local runtime using Jupyter. This allows you to execute code on your local hardware and have access to your local file system.

How do I load a dataset in Google colab from drive?

It is the easiest way to upload a CSV file in Colab. For this go to the dataset in your GitHub repository, and then click on “View Raw”. Copy the link to the raw dataset and pass it as a parameter to the read_csv() in pandas to get the dataframe.

How do I import a text file into Google Colab?

Uploading file using Colab files module You can import files module from google. colab . Then call upload() to launch a “File Upload” dialog and select the file(s) you wish to upload. Once the upload is complete, your file(s) should appear in “Files explorer” and you can read the file as you would normally.


3 Answers

Colab notebooks are stored on Google Drive. But it is run on another virtual machine. So, you need to copy your data.py there too. Do this to upload data.py through Colab.

from google.colab import files
files.upload()
# choose the file on your computer to upload it then
import data
like image 161
korakot Avatar answered Oct 17 '22 19:10

korakot


Now google is officially providing support for accessing and working with Gdrive at ease.

You can use the below code to mount your drive to Colab:

from google.colab import drive
drive.mount('/gdrive')
%cd /gdrive/My\ Drive/{location you want to move}
like image 11
Suraj Donthi Avatar answered Oct 17 '22 18:10

Suraj Donthi


To easily upload a local file you can use the new Google Colab feature:

  • click on right arrow on the left of your screen (below the Google Colab logo) enter image description here
  • select Files tab
  • click Upload button

It will open a popup to choose file to upload from your local filesystem.

like image 11
RomRoc Avatar answered Oct 17 '22 19:10

RomRoc