访问 Colab 的“与我分享”

我想获得访问的文件在谷歌驱动器的“与我共享”目录。

在 Colab python 笔记本中,有以下命令:

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

然而,对于“我的驱动器”目录可以很好地工作

!ls "/content/drive/My Drive"

失败

FileNotFoundError: [ Errno 2]没有这样的文件或目录:

我知道,我可以手动添加文件夹到我的驱动器并继续(就像这里提到的) ,但我想有直接访问共享文件夹,让我说,我可能需要自动化的工作与文件共享与我的实时。

我也意识到其他云平台(如“ OneDrive”)也存在类似的问题。

问题是:

  1. 如果没有直截了当的方法,是否至少有一个 解决办法?
  2. 和我共享代码的人也能进入我的硬盘吗?
95979 次浏览

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.

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:

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!

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.

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'

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

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

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>

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!

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

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

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.

#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

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