dav*_*009 2 python python-3.x google-drive-api
我需要从我的谷歌驱动器下载一个文件,在使用 python 进行一些测试后,我从驱动器下载了数据,就像谷歌文档所说的那样(https://developers.google.com/drive/api/v3/quickstart/python), (https://developers.google.com/drive/api/v3/manage-downloads#downloading_a_file),但我不知道如何将此“数据”保存在文件中。
我怎样才能做到这一点?
这是我的代码
from __future__ import print_function
import pickle
import os.path
import io
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.http import MediaIoBaseDownload
SCOPES = ['https://www.googleapis.com/auth/drive']
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('drive', 'v3', credentials=creds)
file_id = '1ZR7MpJe9KQliuICCv-5iae6DeGQzHaTB'
request = service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print(status)
print ("Download %d%%." % int(status.progress() * 100))
Run Code Online (Sandbox Code Playgroud)
这是我的代码的结果

如果我的理解是正确的,那么这个修改怎么样?
fh = io.BytesIO()
Run Code Online (Sandbox Code Playgroud)
fh = io.FileIO("### filename ###", mode='wb')
Run Code Online (Sandbox Code Playgroud)
如果这不是您想要的结果,我深表歉意。
| 归档时间: |
|
| 查看次数: |
1350 次 |
| 最近记录: |