文件从谷歌驱动器下载到colaboratory [英] file download from google drive to colaboratory

查看:84
本文介绍了文件从谷歌驱动器下载到colaboratory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从Google驱动器中下载文件到colaboratory.

I was trying to download file from my google drive to colaboratory.

file_id = '1uBtlaggVyWshwcyP6kEI-y_W3P8D26sz'

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

downloaded.seek(0)
print('Downloaded file contents are: {}'.format(downloaded.read()))

这样做会收到此错误:

NameError: name 'drive_service' is not defined

如何清除此错误?

推荐答案

将文件从Google驱动器下载到colabo笔记本的最简单方法是通过colabo api:

the easiest method to download a file from google drive to colabo notebook is via the colabo api:

from google.colab import drive
drive.mount('/content/gdrive')

!cp '/content/gdrive/My Drive/<file_path_on_google_drive>' <filename_in_colabo>

备注:

  1. 通过drive.mount(),您可以访问Google驱动器上的任何文件.
  2. 我的云端硬盘"在本地文件系统上等同于"Google云端硬盘".
  3. file_path用单引号引起来,因为安装点(我的驱动器")下方的标准目录中有一个空格,而且无论如何,您的路径中也可能有空格.
  4. 文件浏览器(通过单击左箭头激活)对于查找文件和获取文件路径非常有用.它使您可以单击已安装的文件夹结构并复制文件路径,请参见下图.

这篇关于文件从谷歌驱动器下载到colaboratory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆