我试图将文件从我的google驱动器下载到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
如何删除此错误?
答案 0 :(得分:14)
不安装/导入任何库。只需将文件ID放在末尾即可。
!gdown --id yourFileIdHere
注意:在编写本文时,colab上已预安装gdown库。
答案 1 :(得分:3)
将文件从Google驱动器下载到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>
备注:
答案 2 :(得分:2)
您需要定义驱动器API服务客户端以与Google驱动器API进行交互,例如:
from googleapiclient.discovery import build
drive_service = build('drive', 'v3')
(参见笔记本External data: Drive, Sheets, and Cloud Storage/Drive REST API)
答案 3 :(得分:2)
我建议您使用Pydrive从谷歌硬盘下载文件。我下载500MB数据集5s。 1.安装Pydrive
!pip install PyDrive
2。 OAouth
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
3。谷歌驱动器下载文件的代码
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
Cheer Mo Jihad
答案 4 :(得分:2)
第1步
!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)
Step3
file_id = '17Cp4ZxCYGzWNypZo1WPiIz20x06xgPAt' # URL id.
downloaded = drive.CreateFile({'id': file_id})
downloaded.GetContentFile('shaurya.txt')
Step4
!ls #to verify content
OR
import os
print(os.listdir())
答案 5 :(得分:1)
这是一种轻松的解决方法。您可以在Python中使用wget命令或请求模块来完成工作。
# 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
可以将file_download_link
中的字符串粘贴到浏览器地址栏中,以直接获取下载对话框。
如果使用wget命令:
!wget -O ebook.pdf --no-check-certificate "$file_download_link"
答案 6 :(得分:0)
您还可以在https://github.com/ruelj2/Google_drive的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)