gpt4 book ai didi

python - 如何在google-colaboratory上永久上传数据?

转载 作者:行者123 更新时间:2023-12-01 09:25:35 25 4
gpt4 key购买 nike

当我使用以下代码上传数据时,一旦断开连接,数据就会消失。

from google.colab import files

uploaded = files.upload()

for fn in uploaded.keys():
print('User uploaded file "{name}" with length {length} bytes'.format(
name=fn, length=len(uploaded[fn])))

请建议我上传数据的方法,以便即使在断开连接数天后数据仍保持完整。

最佳答案

我将数据永久存储在 Google 云端硬盘中的 .zip 文件中,并使用以下代码将其上传到 Google Colabs 虚拟机。

将其粘贴到单元格中,然后更改 file_id。您可以从 Google Drive 中文件的 URL 中找到 file_id。 (右键点击文件 -> 获取共享链接 -> 找到打开后的URL部分?id=)

#@title uploader
file_id = "1BuM11fJJ1qdZH3VbQ-GwPlK5lAvXiNDv" #@param {type:"string"}
!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

# 1. Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

# PyDrive reference:
# https://googledrive.github.io/PyDrive/docs/build/html/index.html


from google.colab import auth
auth.authenticate_user()

from googleapiclient.discovery import build
drive_service = build('drive', 'v3')

# Replace the assignment below with your file ID
# to download a different file.
#
# A file ID looks like: 1gLBqEWEBQDYbKCDigHnUXNTkzl-OslSO

import io
from googleapiclient.http import MediaIoBaseDownload

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()

fileId = drive.CreateFile({'id': file_id }) #DRIVE_FILE_ID is file id example: 1iytA1n2z4go3uVCwE_vIKouTKyIDjEq
print(fileId['title'])
fileId.GetContentFile(fileId['title']) # Save Drive file as a local file

!unzip {fileId['title']}

关于python - 如何在google-colaboratory上永久上传数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50426015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com