我在谷歌硬盘的Colab文件夹中已经有价值300-400MB的pickle文件。
我想阅读在Google colab中使用它,但无法做到这一点?
我试过
from google.colab import files
uploaded = files.upload()
#print(uploaded)
for name, data in uploaded.items():
with open(name, 'wb') as f:
#f.write(data)
print ('saved file', name)
但是,它会提示上传。
我已经使用以下方式访问了驱动器:
from google.colab import auth
auth.authenticate_user()
我是否需要再次授予访问权限?
为什么它只在文件夹中显示 datalab ?
$ !ls
> datalab
我是否需要再次将文件重新加载到google colab笔记本?
答案 0 :(得分:0)
您需要使用Python并更改当前目录。例如,
import os
os.chdir('datalab')
将带您进入datalab
文件夹。如果您现在运行!ls
,您将看到datalab
文件夹的内容。然后,您可以根据需要再次更改目录。
答案 1 :(得分:0)
您可以使用pydrive。首先,您需要找到文件的ID。
# Install the PyDrive wrapper & import libraries.
# This only needs to be done once per notebook.
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
# Authenticate and create the PyDrive client.
# This only needs to be done once per notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
# Download a file based on its file ID.
#
# A file ID looks like: laggVyWshwcyP6kEI-y_W3P8D26sz
listed = drive.ListFile({'q': "title contains '.pkl' and 'root' in parents"}).GetList()
for file in listed:
print('title {}, id {}'.format(file['title'], file['id']))
然后您可以使用以下代码加载文件:
from googleapiclient.discovery import build
drive_service = build('drive', 'v3')
import io
import pickle
from googleapiclient.http import MediaIoBaseDownload
file_id = 'laggVyWshwcyP6kEI-y_W3P8D26sz'
request = drive_service.files().get_media(fileId=file_id)
downloaded = io.BytesIO()
downloader = MediaIoBaseDownload(downloaded, request)
done = False
while done is False:
# _ is a placeholder for a progress object that we ignore.
# (Our file is small, so we skip reporting progress.)
_, done = downloader.next_chunk()
downloaded.seek(0)
f = pickle.load(downloaded)
答案 2 :(得分:-1)
我发现在本地安装Google云端硬盘最简单。
from google.colab import drive
drive.mount('/content/gdrive')
!ls # will show you can now access the gdrive locally
这会将您的Google驱动器安装到笔记本电脑,因此您可以访问Google驱动器中的文档,就好像它们是本地文档一样。要访问Google驱动器的“ Colab笔记本”部分,请使用以下路径:
GDRIVE_DIR = "gdrive/My Drive/Colab Notebooks/"
如果您的泡菜文件位于Colab Notebooks文件夹中,则可以使用以下方式加载它们:
import os
import pickle
filename = ... # The name of the pickle file in your Google Drive
data = pickle.load(os.path.join(GDRIVE_DIR, filename))
有关here的信息,请参见有关安装Google云端硬盘和其他方法的教程