1

I'm trying to mount a directory from https://drive.google.com/drive/folders/my_folder_name for use in a google colab notebook.

The instructions for mounting a folder show an example for a directory starting with /content/drive:

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

but my directory doesn't start with /content/drive, and the following things I've tried have all resulted in ValueError: Mountpoint must be in a directory that exists:

drive.mount("/content/drive/folders/my_folder_name")
drive.mount("content/drive/folders/my_folder_name")
drive.mount("drive/folders/my_folder_name")
drive.mount("https://drive.google.com/drive/folders/my_folder_name")

How can I mount a google drive location which doesn't start with /content/drive?

Max Power
  • 8,265
  • 13
  • 50
  • 91
  • You can also mount the whole drive and then save / access files from specific subfolders. – katardin Mar 25 '20 at 01:13
  • 1
    I was wrong, its impossible to mount a subfolder. Just mount the whole drive and then access subfolders in your code. https://stackoverflow.com/questions/53183525/google-colab-how-can-i-mount-a-particular-folder-instead-of-mounting-root-folde – katardin Mar 25 '20 at 01:24

7 Answers7

5

The path in drive.mount('/content/drive') is the path (mount point) where to mount the GDrive inside the virtual box where you notebook is running (refer to 'mount point' in Unix/Linux). It does not point to the path you are trying to access of your Google Drive. Leave "/content/drive" intact and work like this instead:

from google.colab import drive
drive.mount("/content/drive") # Don't change this.

my_path = "/path/in/google_drive/from/root" # Your path
gdrive_path = "/content/drive" + "/My Drive" + my_path # Change according to your locale, if neeeded.
# "/content/drive/My Drive/path/in/google_drive/from/root"

And modify my_path to your desired folder located in GDrive (i don't know if "/My Drive/" changes according to your locale). Now, Colab Notebooks saves notebooks by default in "/Colab Notebooks" so, in MY case, the root of my GDrive is actually gdrive_path = "/content/drive/My Drive" (and I'm guessing yours is too). This leaves us with:

import pandas as pd

from google.colab import drive
drive.mount("/content/drive") # Don't change this.

my_path = "/folders/my_folder_name" # THIS is your GDrive path
gdrive_path = "/content/drive" + "/My Drive" + my_path
# /content/drive/My Drive/folders/my_folder_name

sample_input_file = gdrive_path + "input.csv" # The specific file you are trying to access
rawdata = pd.read_csv(sample_input_file)
# /content/drive/My Drive/folders/my_folder_name/input.csv

After a successul mount, you will be asked to paste a validation code after you have granted permissions to the drive.mount API.

Update: GColab does not require copy/paste of the code anymore but instead to simply confirm you are who you say you are via a usual Google login page.

Sergio
  • 51
  • 1
  • 5
2

You can try this way

drive.mount('/gdrive)

Now access your file from this path

/gdrive/'My Drive'/folders/my_folder_name
Jarvis098
  • 1,420
  • 1
  • 11
  • 16
  • Hi Jarvis, would this work, if the original 'my_folder_name' I got as a shared link that was not in my 'My Drive'? – Max Power Mar 26 '20 at 14:04
1

In my case, this is what worked. I think this is what Katardin suggested, except that I had to first add these subfolders (that I was given access to via a link) to My Drive:

  1. right click on subfolders in the google drive link I was given, and select "Add to My Drive."
  2. Log into my google drive. Add the subfolders to a new folder in my google drive my_folder_name.
  3. Then I could access the contents of those subfolders from colab with the following standard code:
drive.mount('/content/drive')
data_dir = 'drive/My Drive/my_folder_name'
os.listdir(data_dir)  # shows the subfolders I had shared with me
Max Power
  • 8,265
  • 13
  • 50
  • 91
0

I have found the reason why one cant mount ones own google drive for these things is because of a race condition with google . First it was suggested that changing the mount location from /content/gdrive to /content/something else but this didnt fix it. What I ended up doing was copying manually the files that are copied to google drive, then installing the google drive desktop application I would then in windows 10 go to the folder which is now located on google drive and disable file permissions inheritance and then manually putting full control rights on the folder to the users group and to authenticated users group. This seems to have fixed this for me. Other times I have noticed with these colabs (not this one in particular but some of the components used like the trained models are missing from the repository (as if they had been removed) Only solution for this is to look around for other sources of these files. This includes scurrying through google search engine and also looking at the git checkout level to find branches besides master and also looking for projects that cloned the project on github to see if they still include the files.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Hermit
  • 1
  • 1
0
  1. Open the google drive and share the link to everybody or your own accounts.

  2. colab part

from google.colab import drive

drive.mount('/content/drive')
adarsh
  • 6,738
  • 4
  • 30
  • 52
-1

You may want to try the following, though it depends if you're doing this in pro or personal. There is a My Drive that Google Drive keeps in place in the file structure after the /content/drive/.

drive.mount('/content/drive/My Drive/folders/my_folder_name')
katardin
  • 596
  • 3
  • 14
  • 1
    Hey thanks a lot for the answer. This threw `ValueError: Mountpoint must not contain a space.`. Same error trying with double-quotes, and trying to escape the space with `.../My\ Drive/...` – Max Power Mar 25 '20 at 01:22
-1

Copy your Colab document link and open on Chrome incognito window. And run the command again ;) It should work with no error

enter image description here

Peter Csala
  • 17,736
  • 16
  • 35
  • 75