我试图理解为什么对download_file()的此函数调用始终引发错误。有人可以帮我理解为什么会这样吗?
'''
def get_files():
results = service.files().list(
pageSize=10, fields="nextPageToken, files(id, name, mimeType)").execute()
items = results.get('files', [])
return items
def download_file(id, filename, mimeType):
file_id = id
request = get_files()
fh = io.FileIO(filename, 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download {}%.".format(int(status.progress() * 100)))
print("Done")
return fh.getvalue()
def download_all_files():
files = get_files()
for file in files:
id = file['id']
name = file['name']
the_type = file['mimeType']
#print(file['id'], file['name'], file['mimeType'])
download_file(id, name, the_type)
#Calls
download_all_files()
'''
AttributeError:“列表”对象没有属性“ uri”
答案 0 :(得分:1)
您遇到的错误:
AttributeError:“列表”对象没有属性“ uri”
是由于您在in the documentation for such object时将list
对象传递到MediaIoBaseDownload
引起的,它指定第二个参数应为googleapiclient.http.HttpRequest
。
您还要两次调用函数get_files()
,一次调用每个文件,另一次实际获取request
变量(没有太大意义)。
在Google文档中,有一个示例实际上是download a file from Google Drive。在该示例中,python代码如下所示(python 2代码):
file_id = '0BwwA4oUTeiV1UVNwOHItT0xfa2M'
request = drive_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 "Download %d%%." % int(status.progress() * 100)
请参见示例,他们正在调用get_media()
方法,而不是您作为输入传递的文件列表。
基于此,我也将更改您的download_file
函数,如下所示:
def download_file(id, filename, mimeType):
file_id = id
request = drive_service.files().get_media(fileId=id)
fh = io.FileIO(filename, 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download {}%.".format(int(status.progress() * 100)))
print("Done")
return fh.getvalue()
仅作说明,我还要提到id
实际上是built in function of python。所以you shouldn't be using it as variable name