76

Google Colaboratory is really cool, but it would be more useful if I can access all my google drive files, without using standard google drive API.

Is it possible and easy? and how?

Kara
  • 6,115
  • 16
  • 50
  • 57
Marco Scarselli
  • 1,154
  • 2
  • 11
  • 27
  • 1
    Did you manage to find an easy way accessing files/folders directly within Google Drive? I also find this post came after this post of yours: https://stackoverflow.com/questions/48376580/google-colab-how-to-read-data-from-my-google-drive?rq=1, but it is not yet clear to me. – TwinPenguins May 29 '18 at 18:50

7 Answers7

116

Here's an example of using a FUSE Drive interface to access your Drive files like local files: https://colab.research.google.com/notebook#fileId=1srw_HFWQ2SMgmWIawucXfusGzrj1_U0q

In short:

# Load the Drive helper and mount
from google.colab import drive
drive.mount('/content/drive')

After executing the code above, your Drive files will be present in /content/drive/My Drive.

I'm guessing you also found the bundled example I/O notebook, which shows how to use Python APIs to access files as well. (This requires a bit less configuration.) https://colab.research.google.com/notebook#fileId=/v2/external/notebooks/io.ipynb&scrollTo=c2W5A2px3doP

Bob Smith
  • 36,107
  • 11
  • 98
  • 91
  • 1
    not sure how to use this to "map" a folder.. imagine having thousands of individual files in a folder where I'm running a colab notebook that needs to iterate through all files in that folder. – Kai Mar 28 '18 at 18:57
  • i have successfully mapped a file lying inside the Colab Notebooks folder as (a) !cat drive/'Colab Notebooks'/hobbit.txt and as (b) textRDD = spark.sparkContext.textFile("drive/Colab Notebooks/hobbit.txt") – Calcutta Apr 19 '18 at 10:53
34

To mount your Google drive to Colab:

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

and then to cd to any directory in the Google drive:

import os
os.chdir("drive/My Drive/<your-folder>")

and to list all items in the directory that Colab is working on (to make sure it's the right dir):

!ls
Ha Bom
  • 2,787
  • 3
  • 15
  • 29
12

Please follow 3 simple steps in colab notebook to access folder or a file in google drive,

Step :1 Run this snippet

    !apt-get install -y -qq software-properties-common python-software-properties module-init-tools
    !add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
    !apt-get update -qq 2>&1 > /dev/null
    !apt-get -y install -qq google-drive-ocamlfuse fuse
    from google.colab import auth
    auth.authenticate_user()
    from oauth2client.client import GoogleCredentials
    creds = GoogleCredentials.get_application_default()
    import getpass
    !google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
    vcode = getpass.getpass()
    !echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}

At this step you will be asked two times to click on a link to allow access to your drive, at each step code will be generated:(eg:4/AACN9EZG2AU0dRsV0BupjAc107ugSvT_pmr4YPElX7VkoWru6mNmqc8).Paste this code and click enter.

Step:2 To create directory

!mkdir -p drive
!google-drive-ocamlfuse drive

Step:3 Access your file

import pandas as pd
trainDf = pd.read_csv("drive/app/Sample/train.csv");//Here is your file 
Anvesh_vs
  • 376
  • 4
  • 7
7

After success run Drive FUSE program, you can access your drive at /content/drive with using command

import os
os.chdir("/content/drive/")
!ls
sliawatimena
  • 338
  • 1
  • 4
  • 16
5

Yes you can do that.

follow the below steps.

Run the below code and complete the authentication

 !apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!add-apt-repository -y ppa:alessandro-strada/ppa 2>&1 > /dev/null
!apt-get update -qq 2>&1 > /dev/null
!apt-get -y install -qq google-drive-ocamlfuse fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}

**Run the below code **

!mkdir -p drive
!google-drive-ocamlfuse drive

**import file to notebook **

import pandas as pd
pd.read_csv("drive/Colab_Notebooks/4k_without_spcl.csv") 

Colab_Notebooks is folder in google drive

Ravi
  • 2,778
  • 2
  • 20
  • 32
2

Follow these steps:-

  1. Just go on into the files section i.e. the third icon on the right-hand side
  2. Click on the 3rd icon having google drive sign-on it.
  3. An additional block of code will be added to your notebook automatically
  4. Run that block you will get a link in runtime terminal
  5. Click on that it will direct you to the signed-in ids of google
  6. Click on the id of which google drive you want to link
  7. Press OK which will come on the next page
  8. Copy the code on the next page that will come on the next page and paste it in your google Colab runtime terminal

After that "drive" folder will be added to the files section, you can access it through

import os 
folder = os.path.join('/content/drive/My Drive/foldername')
Maheep
  • 767
  • 10
  • 6
0

a tip to always get the code snippet is that you can simply use the client interface provided by Colab:

  1. Go to folders icon side tab enter image description here

  2. Click the icon with Drive's small icon enter image description here

  3. Here you go , you get a suggestion of the code to use: enter image description here

DgrinderHZ
  • 84
  • 4