繁体   English   中英

无法在 colab 笔记本中挂载 Google 驱动器文件夹

[英]Can't mount Google drive folder in colab notebook

我正在尝试从https://drive.google.com/drive/folders/my_folder_name挂载一个目录,以便在 google colab 笔记本中使用。

安装文件夹的说明显示了以 /content/drive 开头的目录的示例:

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

但是我的目录不是以/content/drive开头,并且我尝试过的以下事情都导致了ValueError: Mountpoint must be in a directory that exists

drive.mount("/content/drive/folders/my_folder_name")
drive.mount("content/drive/folders/my_folder_name")
drive.mount("drive/folders/my_folder_name")
drive.mount("https://drive.google.com/drive/folders/my_folder_name")

如何安装不以/content/drive开头的谷歌驱动器位置?

drive.mount('/content/drive')中的路径是在运行笔记本的虚拟盒内安装 GDrive 的路径(安装点)(请参阅 Unix/Linux 中的“安装点”)。 它并不指向您尝试访问 Google Drive 的路径。 保持"/content/drive"不变,并像这样工作:

from google.colab import drive
drive.mount("/content/drive") # Don't change this.

my_path = "/path/in/google_drive/from/root" # Your path
gdrive_path = "/content/drive" + "/My Drive" + my_path # Change according to your locale, if neeeded.
# "/content/drive/My Drive/path/in/google_drive/from/root"

并将my_path修改为位于 GDrive 中的所需文件夹(我不知道"/My Drive/"是否会根据您的语言环境而变化)。 现在,Colab Notebooks 默认将笔记本保存在"/Colab Notebooks"中,所以在我的情况下,我的 GDrive 的根目录实际上是gdrive_path = "/content/drive/My Drive" (我猜你的也是)。 这给我们留下了:

import pandas as pd

from google.colab import drive
drive.mount("/content/drive") # Don't change this.

my_path = "/folders/my_folder_name" # THIS is your GDrive path
gdrive_path = "/content/drive" + "/My Drive" + my_path
# /content/drive/My Drive/folders/my_folder_name

sample_input_file = gdrive_path + "input.csv" # The specific file you are trying to access
rawdata = pd.read_csv(sample_input_file)
# /content/drive/My Drive/folders/my_folder_name/input.csv

成功挂载后,在您授予 drive.mount API 权限后,系统会要求您粘贴验证码。

你可以试试这个方法

drive.mount('/gdrive)

现在从此路径访问您的文件

/gdrive/'My Drive'/folders/my_folder_name

就我而言,这是有效的。 我认为这是 Katardin 的建议,除了我必须首先将这些子文件夹(我可以通过链接访问)添加到“我的驱动器”中:

  1. 右键单击我获得的谷歌驱动器链接中的子文件夹,然后选择“添加到我的驱动器”。
  2. 登录到我的谷歌驱动器。 将子文件夹添加到我的谷歌驱动器my_folder_name的新文件夹中。
  3. 然后我可以使用以下标准代码从 colab 访问这些子文件夹的内容:
drive.mount('/content/drive')
data_dir = 'drive/My Drive/my_folder_name'
os.listdir(data_dir)  # shows the subfolders I had shared with me

我发现一个人无法为这些东西安装自己的谷歌驱动器的原因是因为与谷歌的竞争条件。 首先有人建议将安装位置从 /content/gdrive 更改为 /content/something ,但这并没有解决它。 我最终做的是手动复制复制到谷歌驱动器的文件,然后安装谷歌驱动器桌面应用程序,然后我将在 Windows 10 中转到现在位于谷歌驱动器上的文件夹并禁用文件权限继承,然后手动放置对用户组和经过身份验证的用户组的文件夹的完全控制权限。 这似乎为我解决了这个问题。 其他时候我注意到这些 colabs(不是特别是这个,而是存储库中缺少像训练模型这样使用的一些组件(好像它们已被删除)唯一的解决方案是四处寻找这些的其他来源文件。这包括通过谷歌搜索引擎匆匆忙忙,还查看 git checkout 级别以查找除 master 之外的分支,还查找在 github 上克隆项目的项目以查看它们是否仍然包含文件。

  1. 打开谷歌驱动器并将链接分享给所有人或您自己的帐户。

  2. 合作部分

from google.colab import drive

drive.mount('/content/drive')

您可能想尝试以下方法,但这取决于您是在专业版还是个人版中执行此操作。 Google Drive 在 /content/drive/ 之后的文件结构中保留了一个 My Drive。

drive.mount('/content/drive/My Drive/folders/my_folder_name')

复制您的 Colab 文档链接并在 Chrome 隐身窗口中打开。 并再次运行命令;)它应该可以正常工作

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM