Tensorflow I';ve pickle文件已上载到google drive中。我如何在Google Colab中使用它?

Tensorflow I';ve pickle文件已上载到google drive中。我如何在Google Colab中使用它?,tensorflow,google-drive-api,drive,google-colaboratory,Tensorflow,Google Drive Api,Drive,Google Colaboratory,我在谷歌硬盘的Colab文件夹中已经有了价值300-400MBs的pickle文件 我想在GoogleColab中阅读并使用它,但却无法做到 我试过了 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 ('sa

我在谷歌硬盘的Colab文件夹中已经有了价值300-400MBs的pickle文件

我想在GoogleColab中阅读并使用它,但却无法做到

我试过了

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笔记本?

您需要使用Python并更改当前目录。例如

import os
os.chdir('datalab')

将带您进入
datalab
文件夹。如果您运行
!ls
现在,您将看到
datalab
文件夹的内容。然后,您可以根据需要再次更改目录。

您可以使用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)

我发现在本地安装谷歌硬盘最简单

from google.colab import drive
drive.mount('/content/gdrive')
!ls # will show you can now access the gdrive locally
这会将您的谷歌硬盘安装到笔记本电脑上,这样您就可以像访问本地文档一样访问谷歌硬盘中的文档。要访问google drive的“Colab笔记本”部分,请使用以下路径:

GDRIVE_DIR = "gdrive/My Drive/Colab Notebooks/"
如果您的pickle文件位于Colab笔记本文件夹中,则可以通过以下方式将其加载:

import os
import pickle

filename = ... # The name of the pickle file in your Google Drive
data = pickle.load(os.path.join(GDRIVE_DIR, filename))

有关安装Google Drive和其他方法的教程,请参见

此答案没有提供足够的信息来解决此问题。请考虑原始问题中列出的所有元素。