2

我已经在谷歌驱动器的 Colab 文件夹中有价值 300-400MB 的泡菜文件。

我想在 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 notebook 吗?

4

3 回答 3

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)
于 2019-02-17T12:18:21.340 回答
0

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

import os
os.chdir('datalab')

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

于 2018-02-10T17:45:49.987 回答
0

我发现在本地安装 Google Drive 最简单。

from google.colab import drive
drive.mount('/content/gdrive')
!ls # will show you can now access the gdrive locally

这会将您的 google 驱动器安装到笔记本上,因此您可以访问 google 驱动器中的文档,就好像它们是本地的一样。要访问 Google 驱动器的“Colab Notebooks”部分,请使用以下路径:

GDRIVE_DIR = "gdrive/My Drive/Colab Notebooks/"

如果您在 Colab Notebooks 文件夹中有您的 pickle 文件,那么您可以使用以下命令加载它们:

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 和其他方法的教程

于 2019-02-17T13:53:54.583 回答