问题很简单:我在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)

其他回答

我很懒,我的记忆力很差,所以我决定创建一个更容易记忆和输入的easycolab:

import easycolab as ec
ec.mount()

确保首先安装它:!pip install easycolab

mount()方法基本上实现了这一点:

from google.colab import drive
drive.mount(‘/content/drive’)
cd ‘/content/gdrive/My Drive/’

我所做的是:

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

Then

%cd /content/drive/My Drive/Colab Notebooks/

之后我就可以读取csv文件了

df = pd.read_csv("data_example.csv")

如果文件的位置不同,只需在“我的驱动器”后添加正确的路径

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

这对我来说是完美的 我后来能够使用操作系统库来访问我的文件,就像我在我的PC上访问它们一样

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

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

运行代码并复制粘贴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)

不能在colab上永久存储文件。虽然你可以从你的驱动器导入文件,每次当你完成了文件,你可以把它保存回来。

将谷歌驱动器挂载到Colab会话

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

您可以像写入本地文件系统一样简单地写入谷歌驱动器 现在如果你看到你的谷歌驱动器将加载在文件选项卡。现在您可以从您的colab访问任何文件,您可以写入以及读取它。这些更改将在您的驱动器上实时完成,任何拥有您文件访问链接的人都可以从您的colab查看您所做的更改。

例子

with open('/content/gdrive/My Drive/filename.txt', 'w') as f:
   f.write('values')