我正在开发一个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()
是否可以删除选定的文件(毕竟,这是一项基本操作,所以我不明白为什么无法这样做)?
提前致谢!
您可以将多个Drive API请求一起批处理。这样的事情应该可以在Python API客户端库中使用:
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)