如何使用Google云端硬盘API一次删除多个文件

纳达夫96

我正在开发一个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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从Google云端硬盘删除文件

如何使用Python读取多个Google云端硬盘CSV文件,然后附加到一个文件中?

如何使用Dask从Google云端存储中读取多个大型CSV文件的块,而不会一次使所有内存过载

使用Google Apps脚本从Google云端硬盘删除文件

Google Drive API“保存到云端硬盘”多个文件

使用Google云端硬盘SDK删除与我共享的文件

如何在Drive API for Javascript中一次删除多个文件?

Google云端存储:一次上传很多小文件?

Google云端硬盘API如何找到文件的路径?

从Google云端硬盘永久删除文件

Pydrive从Google云端硬盘删除文件

如何使用For Loop通过API从Google云端硬盘下载文件

如何使用Google云端硬盘API(v3)获取目录中所有文件的列表

如何使用Drive.API从Google云端硬盘下载文件?

如何使用Python和Drive API v3下载Google云端硬盘文件

如何仅使用Rest API在Response中将文件与Google云端硬盘分开

如何在Google云端硬盘上强制使用唯一的文件名?

使用Google Apps脚本:如何转换/导出云端硬盘文件?

如何使用gcp从Google云端硬盘复制文件

如何对来自Google云端硬盘的文件使用importXML函数?

如何使用Google云端硬盘获取文件修改日期

使用多个参数搜索Google云端硬盘

当我通过google.api.drive请求文件列表时,我返回一个pdf文件“如何开始使用云端硬盘”

如何一次删除多个列

如何使用终端一次重命名多个文件?

如何使用UIDocumentPickerViewController一次导入多个文件?

如何使用 TIdHTTP 一次下载多个文件

如何从Google云端硬盘读取txt文件?

如何使用REST API创建文件选择器以将文件保存到Google云端硬盘?