Google colaboratory 从google drive下载文件到colaboratory
我正试图从我的谷歌硬盘下载文件到colaboratoryGoogle colaboratory 从google drive下载文件到colaboratory,google-colaboratory,Google Colaboratory,我正试图从我的谷歌硬盘下载文件到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 = MediaIoBaseDo
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
如何删除此错误?您需要定义一个驱动器API服务客户端以与Google驱动器API交互,例如:
from googleapiclient.discovery import build
drive_service = build('drive', 'v3')
(参见笔记本)我建议您使用Pydrive从google drive下载文件。我下载500MB的数据集供5s使用。 1.安装Pydrive
!pip install PyDrive
二,。奥思
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
三,。从谷歌硬盘下载文件的代码
fileId = drive.CreateFile({'id': 'DRIVE_FILE_ID'}) #DRIVE_FILE_ID is file id example: 1iytA1n2z4go3uVCwE_vIKouTKyIDjEq
print fileId['title'] # UMNIST.zip
fileId.GetContentFile('UMNIST.zip') # Save Drive file as a local file
为圣战欢呼第一步
!pip install -U -q PyDrive
步骤2
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)
步骤3
file_id = '17Cp4ZxCYGzWNypZo1WPiIz20x06xgPAt' # URL id.
downloaded = drive.CreateFile({'id': file_id})
downloaded.GetContentFile('shaurya.txt')
步骤4
!ls #to verify content
或
您还可以使用我在google.colab和PyDrive上的实现,这样做会更简单
!pip install - U - q PyDrive
import os
os.chdir('/content/')
!git clone https://github.com/ruelj2/Google_drive.git
from Google_drive.handle import Google_drive
Gd = Google_drive()
Gd.load_file(local_dir, file_ID)
将文件从google drive下载到colabo笔记本的最简单方法是通过colabo api:
from google.colab import drive
drive.mount('/content/gdrive')
!cp '/content/gdrive/My Drive/<file_path_on_google_drive>' <filename_in_colabo>
从google.colab导入驱动器
安装(“/content/gdrive”)
!cp'/content/gdrive/My Drive/'
备注:
这里有一个简单的方法。您可以使用Python中的wget命令或requests模块来完成工作
# find the share link of the file/folder on Google Drive
file_share_link = "https://drive.google.com/open?id=0B_URf9ZWjAW7SC11Xzc4R2d0N2c"
# extract the ID of the file
file_id = file_share_link[file_share_link.find("=") + 1:]
# append the id to this REST command
file_download_link = "https://docs.google.com/uc?export=download&id=" + file_id
文件下载链接中的字符串可以粘贴到浏览器地址栏中,直接进入下载对话框
如果使用wget命令:
!wget -O ebook.pdf --no-check-certificate "$file_download_link"
不安装/导入任何库。把你的文件id放在最后
!gdown --id yourFileIdHere
注意:在编写本文时,gdown库是预先安装在colab上的
!gdown --id yourFileIdHere