解决了。
首先按照文档中的说明进行简单的身份验证
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
# 1. Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
如果您知道文件在驱动器上的文件名和文件夹 ID,我创建了一些帮助函数来获取文件 ID。文件夹 ID 是 drive.google.com/../../folders/ 中链接的最后一部分
def get_file_from_drive(folder_id, file_name):
file_list = drive.ListFile({'q': "'" + folder_id + "' in parents and
trashed=false"}).GetList()
for file in file_list:
if file['title'] == file_name:
return file['id']
def upload_file_to_drive(file_name, file_data):
uploaded = drive.CreateFile({'title': file_name})
uploaded.SetContentString(file_data)
uploaded.Upload()
print('Uploaded file with ID {}'.format(uploaded.get('id')))
drive_folder_id = '<Folder ID>'
此功能将文件从google drive上传到colab允许您使用的虚拟系统中。
def upload_data_system():
downloaded = drive.CreateFile({'id': get_file_from_drive(drive_folder_id, 'train.npy')})
downloaded.GetContentFile('train.npy')
downloaded = drive.CreateFile({'id': get_file_from_drive(drive_folder_id, 'train_labels.npy')})
downloaded.GetContentFile('train_labels.
upload_data_system()
维奥拉!您的文件已上传到文件系统,并且可以使用简单的 python 加载到内存中,就像在本地完成一样。要验证,请在 colab 上运行它。你应该看到你的文件
import os
from os import listdir
for f in os.listdir('.'):
if os.path.isfile(f):
print(f)
现在将您的 numpy 文件加载为 np.load(path_to_file_in_filesystem)