【问题标题】:Can't mount Google drive folder in colab notebook无法在 colab 笔记本中挂载 Google 驱动器文件夹
【发布时间】:2022-02-22 14:39:41
【问题描述】:

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

安装文件夹的说明显示了以 /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 开头的谷歌驱动器位置?

【问题讨论】:

  • 您也可以挂载整个驱动器,然后保存/访问特定子文件夹中的文件。
  • 我错了,不可能挂载子文件夹。只需安装整个驱动器,然后访问代码中的子文件夹。 stackoverflow.com/questions/53183525/…

标签: python google-drive-api google-colaboratory mount-point


【解决方案1】:

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 权限后,系统会要求您粘贴验证码。

【讨论】:

    【解决方案2】:

    你可以试试这个方法

    drive.mount('/gdrive)
    

    现在从这个路径访问你的文件

    /gdrive/'My Drive'/folders/my_folder_name
    

    【讨论】:

    • 您好 Jarvis,如果我获得的原始“my_folder_name”作为共享链接而不在“我的云端硬盘”中,这是否可行?
    【解决方案3】:

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

    1. 右键单击我获得的 Google Drive 链接中的子文件夹,然后选择“添加到我的驱动器”。
    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
    

    【讨论】:

    • 如果这对您有用,也许您应该将其标记为答案。通过这种方式,它可以帮助社区中的其他人。
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:
      1. 打开谷歌驱动器并将链接分享给所有人或您自己的帐户。

      2. 合作部分

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

      【讨论】:

        【解决方案6】:

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

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

        【讨论】:

        • 嘿,非常感谢您的回答。这抛出了ValueError: Mountpoint must not contain a space.。尝试使用双引号并尝试使用 .../My\ Drive/... 转义空格时出现同样的错误
        【解决方案7】:

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

        【讨论】:

          猜你喜欢
          • 2020-11-15
          • 1970-01-01
          • 2020-10-01
          • 2021-01-13
          • 1970-01-01
          • 2021-10-17
          • 2019-12-17
          • 2019-04-10
          • 2023-03-26
          相关资源
          最近更新 更多