accessing “Shared with me” with Colab
我想访问Google云端硬盘"与我共享"目录中的文件。
在Colab python笔记本中,以下命令:
1 2 3 4 | import os from google.colab import drive drive.mount('/content/drive') !ls"/content/drive/My Drive" |
对于"我的云端硬盘"目录来说效果很好
1 | !ls"/content/drive/My Drive" |
与
失败
FileNotFoundError: [Errno 2] No such file or directory:
我知道我可以手动将文件夹添加到驱动器并继续操作(如此处所述),但是我想直接访问共享文件夹,以便说我可能需要自动化工作与我实时共享的文件。
我也知道其他云平台(例如" OneDrive")存在相同/相似的问题。
问题是:
解决方法?
RE:是否有解决方法-
在Web UI中加载共享文件,右键单击所需目录,然后选择"添加到我的云端硬盘"。然后,该文件夹将出现在您希望的
就上下文而言,云端硬盘与普通的文件系统不同:文件和目录可以具有多个父目录,因此会同时出现在文件列表和原始所有者中。
RE:其他用户是否可以访问云端硬盘文件? -
否,笔记本是Drive中具有不同Drive权限的不同对象。
另一种解决方法是转到Google云端硬盘,右键单击有问题的文件夹,然后单击"向云端硬盘添加快捷方式"。这将允许您从驱动器访问该文件夹!
do the people I share my code with get access to my drive too?
否,他们只能访问您与他们共享的内容(例如,您正在使用的笔记本)。有关权限的更多信息,请参见:
找到了与您访问共享目录内容的解决方案
从您的Google云端硬盘与我共享。
找到目录,右键单击添加快捷方式,然后指定路径以获取保存快捷方式
安装驱动器
然后转到您指定的快捷方式位置,然后键入
1 | ls |
您可以看到快捷方式并通过
访问它
我通过将main / top文件夹添加为驱动器的快捷方式来解决此问题,然后使用了类似您的代码
我的案子:
我想从与我共享的文件夹中的文件夹中加载一个csv文件,因此在我的gdrive中,它将位于"与我共享/folder_0/folder_1/file.csv"中。
因此我转到文件夹_0的右下角,单击"向驱动器添加快捷方式"->"我的驱动器"->"添加快捷方式"。现在,应该将整个folder_0作为快捷方式添加到驱动器中。
最后在colab中nb:
1 2 3 4 5 | # 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") |
在我的情况下,
是一个在熊猫中加载的csv文件,但是这里重要的是路径
1 2 3 4 | import os from google.colab import drive drive.mount('/content/drive',force_remount=True) os.chdir("/content/drive/.shortcut-targets-byid/10xasdbasbdiluabsdiubiuadWEWEdaq/SHAREDFOLDER") |
现在,所有团队都可以使用相同的代码(所有者需要与团队共享SHAREDFOLDER)
如果要将文件从共享目录硬拷贝到自己的驱动器中,请执行以下操作:
- 如上所述安装驱动器
- 在我自己的目录中创建共享文件夹的快捷方式或软链接
- 然后,如果您" ls"驱动器,您应该会看到类似这样的信息
"新快捷方式"->
- 我创建了一个新的本地目录,我想要所有内容
- 我" cd"到该目录
-
我将共享文件夹的内容复制到了本地新创建的dir
中
cp -rp '/gdrive/.shortcut-targets-by-id/dffdffxxxxxxxxxx/[shared folder]/* 。
效果很好。
一种解决方法是以下方法从Google驱动器导入共享文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # 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") |
我认为是错字
1 2 3 4 | import os from google.colab import drive drive.mount('/content/gdrive') !ls"/content/gdrive/My Drive" |
应该是正确的。
您错过了'g'