如何使用 Google Drive API 一次删除多个文件

Nad*_*v96 8 python google-api delete-file google-drive-api google-api-python-client

我正在开发一个 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() 
Run Code Online (Sandbox Code Playgroud)

是否可以删除选定的文件(我不明白为什么不可能,毕竟这是一项基本操作)?

提前致谢!

abr*_*ham 5

您可以一起批处理多个 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)
Run Code Online (Sandbox Code Playgroud)