MemoryError-如何使用Python通过Google Drive SDK下载大文件
作者:互联网
从Google云端硬盘下载大文件时,我的内存不足.
我假设tmp = content.read(1024)不起作用,但是如何解决?
谢谢.
def download_file(service, file_id):
drive_file = service.files().get(fileId=file_id).execute()
download_url = drive_file.get('downloadUrl')
title = drive_file.get('title')
originalFilename = drive_file.get('originalFilename')
if download_url:
resp, content = service._http.request(download_url)
if resp.status == 200:
file = 'tmp.mp4'
with open(file, 'wb') as f:
while True:
tmp = content.read(1024)
if not tmp:
break
f.write(tmp)
return title, file
else:
print 'An error occurred: %s' % resp
return None
else:
return None
解决方法:
正确的解决方案是实施部分下载.通过此过程,您将请求文件长度不同的块,直到完全下载为止.这是该过程的参考:https://developers.google.com/drive/web/manage-downloads#partial_download.
标签:large-data,python,google-drive-api 来源: https://codeday.me/bug/20191013/1907970.html