如何使用Google Drive API一次删除多个文件
作者:互联网
我正在开发一个python脚本,它将文件上传到我的驱动器中的特定文件夹,因为我注意到,驱动器api提供了一个很好的实现,但我确实遇到了一个问题,如何一次删除多个文件?
我尝试从驱动器中抓取我想要的文件并组织他们的Id但在那里没有运气……(下面的一个片段)
dir_id = "my folder Id"
file_id = "avoid deleting this file"
dFiles = []
query = ""
#will return a list of all the files in the folder
children = service.files().list(q="'"+dir_id+"' in parents").execute()
for i in children["items"]:
print "appending "+i["title"]
if i["id"] != file_id:
#two format options I tried..
dFiles.append(i["id"]) # will show as array of id's ["id1","id2"...]
query +=i["id"]+", " #will show in this format "id1, id2,..."
query = query[:-2] #to remove the finished ',' in the string
#tried both the query and str(dFiles) as arg but no luck...
service.files().delete(fileId=query).execute()
是否可以删除所选文件(我不明白为什么它不可能,毕竟这是一个基本操作)?
提前致谢!
解决方法:
你可以一起batch multiple Drive API requests.这样的东西应该使用Python API Client Library:
def delete_file(request_id, response, exception):
if exception is not None:
# Do something with the exception
pass
else:
# Do something with the response
pass
batch = service.new_batch_http_request(callback=delete_file)
for file in children["items"]:
batch.add(service.files().delete(fileId=file["id"]))
batch.execute(http=http)
标签:python,delete-file,google-api,google-drive-sdk,google-api-python-client 来源: https://codeday.me/bug/20190628/1314204.html