问题很简单:我在gDrive上有一些数据,例如在 /项目/ my_project / my_data *。

我也有一个简单的笔记本在gColab。

所以,我想做的是:

for file in glob.glob("/projects/my_project/my_data*"):
    do_something(file)

不幸的是,所有的例子(例如https://colab.research.google.com/notebook#fileId=/v2/external/notebooks/io.ipynb)都建议只将所有必要的数据加载到笔记本中。

但是,如果我有很多数据,就会很复杂。 有没有解决这个问题的机会?

谢谢你的帮助!


当前回答

我写了一个类来下载所有的数据到。’在colab服务器中的位置

整个事情可以从这里拉https://github.com/brianmanderson/Copy-Shared-Google-to-Colab

!pip install PyDrive


from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
import os

class download_data_from_folder(object):
    def __init__(self,path):
        path_id = path[path.find('id=')+3:]
        self.file_list = self.get_files_in_location(path_id)
        self.unwrap_data(self.file_list)
    def get_files_in_location(self,folder_id):
        file_list = drive.ListFile({'q': "'{}' in parents and trashed=false".format(folder_id)}).GetList()
        return file_list
    def unwrap_data(self,file_list,directory='.'):
        for i, file in enumerate(file_list):
            print(str((i + 1) / len(file_list) * 100) + '% done copying')
            if file['mimeType'].find('folder') != -1:
                if not os.path.exists(os.path.join(directory, file['title'])):
                    os.makedirs(os.path.join(directory, file['title']))
                print('Copying folder ' + os.path.join(directory, file['title']))
                self.unwrap_data(self.get_files_in_location(file['id']), os.path.join(directory, file['title']))
            else:
                if not os.path.exists(os.path.join(directory, file['title'])):
                    downloaded = drive.CreateFile({'id': file['id']})
                    downloaded.GetContentFile(os.path.join(directory, file['title']))
        return None
data_path = 'shared_path_location'
download_data_from_folder(data_path)

其他回答

使用colab笔记本从谷歌驱动器读取图像

import glob
images_list = glob.glob("add google drive path/*.jpg")
print(images_list)

创建YOLOv4培训所需的training.txt文件

file = open("/content/drive/MyDrive/project data/obj/train.txt", "w") 
file.write("\n".join(images_list)) 
file.close() 

您可以简单地使用屏幕左侧的代码片段。 在这里输入图像描述

插入“在虚拟机中挂载谷歌驱动器”

运行代码并复制粘贴URL中的代码

然后使用!ls检查目录

!ls /gdrive

在大多数情况下,你会在“/gdrive/My drive”目录下找到你想要的东西。

你就可以这样行:

from google.colab import drive
drive.mount('/gdrive')
import glob

file_path = glob.glob("/gdrive/My Drive/***.txt")
for file in file_path:
    do_something(file)

编辑:截至2020年2月,现在有一个自动挂载驱动器的一流UI。

首先,打开左边的文件浏览器。它会显示一个“Mount Drive”按钮。一旦点击,你会看到一个权限提示来挂载驱动器,然后当你返回笔记本电脑时,你的驱动器文件就会出现,没有任何设置。完成的流程如下所示:

原始答案如下。(这同样适用于共享笔记本电脑。)

您可以通过运行以下代码片段挂载您的谷歌驱动器文件:

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

然后,您可以在文件浏览器侧面板或使用命令行实用程序与您的Drive文件进行交互。

这是一个笔记本的例子

之前的大多数答案都有点(非常)复杂,

from google.colab import drive
drive.mount("/content/drive", force_remount=True)

我发现这是最简单和最快的方法来安装谷歌驱动器到CO实验室,你可以改变挂载目录的位置,只要改变drive.mount的参数。它会给你一个链接,接受与您的帐户的权限,然后你必须复制粘贴生成的密钥,然后驱动器将被安装在选定的路径。

Force_remount仅在必须挂载驱动器时使用,而不管之前是否加载了驱动器。如果不想强制挂载,可以忽略这个when参数

编辑:查看这篇文章,了解更多在colab https://colab.research.google.com/notebooks/io.ipynb中执行IO操作的方法

读取文件夹中的所有文件:

import glob
from google.colab import drive
drive.mount('/gdrive', force_remount=True)

#!ls "/gdrive/My Drive/folder"

files = glob.glob(f"/gdrive/My Drive/folder/*.txt")
for file in files:  
  do_something(file)