43

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?
SE_net4 the downvoter
  • 21,043
  • 11
  • 69
  • 107
Leon Rai
  • 681
  • 1
  • 6
  • 15

11 Answers11

28

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.

Bob Smith
  • 26,929
  • 9
  • 72
  • 69
17

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!

Pranav Pillai
  • 191
  • 1
  • 5
  • 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. – kawingkelvin May 23 '20 at 16:04
  • This worked for me. Thank you very much. – Chamod May 16 '21 at 08:23
13

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

diego-florez
  • 131
  • 1
  • 5
5

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:

EngrSMukhtar
  • 51
  • 1
  • 2
5

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>

Susaj S N
  • 576
  • 4
  • 18
3

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.

NIMISHAN
  • 1,253
  • 3
  • 19
  • 27
FoxMan
  • 41
  • 4
2
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)

1

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'

1

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!

hansrajSwapnil
  • 175
  • 2
  • 10
0

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")
Karthik
  • 21
  • 9
-1

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