accessing "Shared with me" with Colab
Asked Answered
D

16

126

I want to get access to the files in the Google Drive's "Shared with me" directory.

In the Colab python notebook the following commands:

import os
from google.colab import drive
drive.mount('/content/drive')
!ls "/content/drive/My Drive"

work well for "My Drive" directory however

!ls "/content/drive/My Drive"

fails with

FileNotFoundError: [Errno 2] No such file or directory:

I am aware of the fact that I can add the folder to my drive manually and proceed (as mentioned in here), but I would like to have direct access to the shared folders for lets say I might need to automate the work with files that are shared with me real time.

I am also aware that same/similar problem exist with other cloud platforms like "OneDrive".

The questions are:

  1. if there is no straightforward way of doing it, is there at least a workaround?
  2. do the people I share my code with get access to my drive too?
Dehydrogenate answered 24/1, 2019 at 17:0 Comment(3)
ls "/content/drive/My Drive" doesn't look like a valid python statementNaomanaomi
my apologies, you're right, I meant to say "in the python notebook". Updated, thanks for noticing.Dehydrogenate
Shouldn't the line ls "/content/drive/My Drive" have an exclamation mark before it, since we are in a notebook context?Vietnam
I
108

Another work around is go to Google Drive, right click on the folder in question, and then click "Add shortcut to Drive". This will allow you to access the folder from your drive!

Inappreciable answered 9/4, 2020 at 3:21 Comment(1)
I have a folder with many subfolders and hierarchies. It seems that random files and folders are not available if i drill into this shortcut from drive. Trying to access them through "My Drive" (after mounting on colab) also doesnt work. The missing files/folders seem to be random.Graces
M
43

RE: Is there a work-around --

Load your shared files in the web UI, right click on the directory of interest, and select 'Add to my Drive'. Then, the folder will appear in /content/drive/My Drive as you hope.

For context, Drive isn't like a normal filesystem: files and directories can have multiple parents, thereby appearing in both your file list and the original owners.

RE: Will other users have access to Drive files? --

No, the notebook is a distinct object in Drive with distinct Drive permissions.

Midrash answered 24/1, 2019 at 17:15 Comment(1)
Don't see the option 'Add to my Drive' but there is option for 'Add shortcut to Drive'.Remittee
M
29

I solved by adding the main/top folder as shortcut to my drive, and then I used a code like yours

My case:

I wanted to load a csv file from a folder which was in a folder shared with me, so in my gdrive would be in "Shared with me/folder_0/folder_1/file.csv"

So I went to folder_0 right bottom click "Add shortcut to Drive" --> "My Drive" --> "ADD SHORTCUT". Now the whole folder_0 should be added to your drive as a shortcut.

Finally in the colab nb:

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

pandas.read_csv("/content/drive/My Drive/folder_0/folder_1/file.csv")

In my case is a csv file loaded in pandas, but what matters here is the path "/content/drive/My Drive/folder_0/folder_1/file.csv", where "/content/drive/My Drive/" is always like this and "folder_0/folder_1/file.csv" is the part that must be equal to the shorcut path you just added

Mcinerney answered 11/8, 2020 at 20:7 Comment(1)
Hi @diego-florez, thanks for your answer. Could you edit it in order to clarify your solution? Thanks!Corneliuscornell
M
20

You Can Create A Shortcut from your 'Share With Me' files to your Drive it works for me:

Steps

  1. open 'Share With Me' on your drive
  2. select the folder or file click the right button on it and select add shortcut then select the path in 'My Drive' fff
Molality answered 20/5, 2021 at 15:38 Comment(2)
It works for me. You need to select the path that you want to access, click the right button on it and select add shortcut. Now, this path shows at root of your Drive.Varico
I got to use it both for the shared drive and for the drive. It works normally. I recommend if you want to save the images, create a copy of the images. That avoids error in case of changeEndamoeba
S
16

In order to access a shared with you folder or file in Google Colab you have to:

  1. Go to Shared with me in Google Drive.
  2. Select the folder or file you want to acess.
  3. Right click on it and choose Add shortcut to drive.
  4. A pop-up window will apear, Select MyDrive then click on Add Shortcut.
  5. Now, Go to your Google Colab Notebook and mount to Google Drive with:
from google.colab import drive

drive.mount("/content/gdrive")
  1. Let's suppose you want to access a folder called 'Dataset', you can do this with:
dataset_dir = '/content/gdrive/My Drive/Dataset'
  1. Now you are ready to access and work on this folder.
Strage answered 1/12, 2021 at 9:2 Comment(1)
The shortcut trick worked! Thank you!Trisect
V
8

Found a solution for accessing contents of a shared directory with you

  1. Go to shared with me from your google drive.

  2. Find the directory, right click add shortcut then specify the path get save the shortcut

  3. mount the drive

    from google.colab import drive drive.mount('/content/drive',force_remount=True)

    import os

    os.chdir("/content/drive/My Drive")

And go to the shortcut location where you specified and type

ls

you can see the shortcut and access it via cd <directory>

Vanir answered 17/8, 2020 at 5:20 Comment(1)
Making a shortcut FOR THE WIN!Slesvig
M
6

do the people I share my code with get access to my drive too?

No, they can only access what you shared with them (eg, the notebook you're working on). more info on permissions here:

Mycenae answered 2/11, 2019 at 7:36 Comment(0)
L
6

If you want to hard copy the files form a Shared directory to your own Drive I did as follows:

  • mounted the drive as described above
  • created a shortcut or soft-link of the shared folder in my own dir
  • then if you "ls" your drive you should see something like this

"New shortcut" -> /gdrive/.shortcut-targets-by-id/dffdffxxxxxxxxxx/[shared folder]

  • I created a new local directory where I want all the content
  • I "cd" to that directory
  • I copied the content of the shared folder into my local newly created dir

    cp -rp '/gdrive/.shortcut-targets-by-id/dffdffxxxxxxxxxx/[shared folder]/*.

It worked perfectly.

Lianna answered 28/5, 2020 at 1:12 Comment(0)
A
6
import os
from google.colab import drive
drive.mount('/content/drive',force_remount=True)
os.chdir("/content/drive/.shortcut-targets-byid/10xasdbasbdiluabsdiubiuadWEWEdaq/SHAREDFOLDER")

Now the same code will work for all team (owner need to share the SHAREDFOLDER with team)

Atoll answered 24/7, 2020 at 16:18 Comment(0)
P
3

I think it is typo

import os
from google.colab import drive
drive.mount('/content/gdrive')
!ls "/content/gdrive/My Drive"

should be right.

you missed 'g'

Psittacosis answered 28/5, 2020 at 9:21 Comment(0)
C
2

This is a hack which worked fine for me. I had to load glove vectors file which was shared with me and was in the "Shared with me" folder in my google drive.

Right click on the file -> select Add Shortcut to Drive -> select any location in Drive/My Drive.

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

above code snippet will mount your gdrive in the colab notebook. Click on the the mounted folders to find out your file-> right click -> copy path

Now, you can read any file using the path you just copied!

Clos answered 19/1, 2021 at 21:59 Comment(0)
C
2

To access a shared file from colab, you can create a shortcut to the file in the MyDrive folder by moving it into the folder. You can then access it with the path: /content/drive/MyDrive/filename

Coelho answered 24/2, 2023 at 20:15 Comment(0)
I
1

One work around is the following approach to import shared files from google drive.

# Install PyDrive
!pip install PyDrive

#Import modules
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

#Authenticate and create the PyDrive client
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

#Get the Shareable link
#Ex link : https://drive.google.com/file/d/1c7Ffo1Go1dtUpKcSWxdbdVyW4dfhEoUp/view?usp=sharing
# Get the id from the link 1c7Ffo1Go1dtUpKcSWxdbdVyW4dfhEoUp
downloaded = drive.CreateFile({'id':"your_file_ID"})   
downloaded.GetContentFile('your_file_name.csv')       

#Read data
import pandas as pd
df = pd.read_csv("your_file_name.csv")
Illinium answered 24/6, 2020 at 6:37 Comment(0)
P
1

----easiest way!---- if you need for all members to access this file, I recommend that all of them make a folder (named something like "shortcuts") and do the following:

  1. find 'path' that is shared with you
  2. right click the file/folder in 'path' and choose 'Add shortcut to drive'
  3. from 'MyDrive' choose 'shortcuts' folder and then click 'Add Shortcut'

finally with these lines of code, all members can access and mount the same data shared with them.

from google.colab import drive
drive.mount('/content/drive', force_remount=True)

data_path = '/content/drive/MyDrive/shortcuts/FileOrFolderName'

you can use data_path where ever you want in your code!

Philbert answered 9/9, 2021 at 13:2 Comment(1)
To make this answer clearer, please use the list formatting. Use this link: stackoverflow.com/editing-help#simple-listsStemson
A
1

Another work around: Click on the folder you want to add to my drive. Then Shift+z and select where to add the folder to. More information here

Amusing answered 5/2, 2022 at 8:44 Comment(0)
M
0

#Step 1:

Mount the google drive into your colab notebook

from google.colab import drive 
drive.mount('/content/drive',force_remount=True)
import os 
import time

#Step 2:

change directory to your drive

os.chdir("/content/drive/MyDrive/")

#Step 3:

Make a new directory where your files should be stored

!mkdir copyithere

#Step 4:

Go back to the shared folder and make a shortcut to the current "copyithere" folder

#Step 5:

rename the shortcut to a name that doesn't include space

!mv "your shortcut name" targettobecopied

#Step 6:

Move into the the renamed shortcut "targettobecopied"

os.chdir("/content/drive/MyDrive/copyithere/targettobecopied")

#Step 7:

Now copying from the shortcut to your actual google drive

%%time 
!yes | cp -R -u -p -r -n * /content/drive/MyDrive/copyithere

~ Reda

Manslayer answered 12/1, 2022 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.