问题很简单:我在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上永久存储文件。虽然你可以从你的驱动器导入文件,每次当你完成了文件,你可以把它保存回来。
将谷歌驱动器挂载到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')