accessing "Shared with me" with Colab

PythonGoogle ColaboratoryShared DirectoryFlysystem Google-Drive

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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!

Solution 2 - Python

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.

Solution 3 - Python

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

Solution 4 - Python

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

Solution 5 - Python

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>

Solution 6 - Python

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.

Solution 7 - Python

> 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:

Solution 8 - Python

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.

Solution 9 - Python

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)

Solution 10 - Python

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'

Solution 11 - Python

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!

Solution 12 - Python

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")

Solution 13 - Python

----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!

Solution 14 - Python

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

Solution 15 - Python

#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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLeon RaiView Question on Stackoverflow
Solution 1 - PythonPranav PillaiView Answer on Stackoverflow
Solution 2 - PythonBob SmithView Answer on Stackoverflow
Solution 3 - Pythondiego-florezView Answer on Stackoverflow
Solution 4 - PythonAhmed AbdalrhmanView Answer on Stackoverflow
Solution 5 - PythonSusaj S NView Answer on Stackoverflow
Solution 6 - PythonBaya LinaView Answer on Stackoverflow
Solution 7 - PythonEngrSMukhtarView Answer on Stackoverflow
Solution 8 - PythonFoxManView Answer on Stackoverflow
Solution 9 - PythonAmit SaxenaView Answer on Stackoverflow
Solution 10 - Pythonuser10354223View Answer on Stackoverflow
Solution 11 - PythonhansrajswapnilView Answer on Stackoverflow
Solution 12 - PythonKarthik SelvamView Answer on Stackoverflow
Solution 13 - Pythonamirhossein soleimanyView Answer on Stackoverflow
Solution 14 - PythonJane KathambiView Answer on Stackoverflow
Solution 15 - PythonReda MastouriView Answer on Stackoverflow