我正在开发一个python脚本,它将把文件上传到我的驱动器中的一个特定文件夹,正如我注意到的那样,驱动器api提供了一个很好的实现,但是我确实遇到了一个问题,我如何一次删除多个文件?
我试着从驱动器上抓取我想要的文件,整理他们的身份证,但那里没有运气.(下面是一个片段)
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() 是否可以删除选定的文件(我不明白为什么不可能,毕竟,这是一个基本的操作)?
提前感谢!
发布于 2015-11-16 03:12:15
如果您的delete或trash文件夹,它将递归删除/删除该文件夹中包含的所有文件。因此,您的代码可以大大简化:
dir_id = "my folder Id"
file_id = "avoid deleting this file"
service.files().update(fileId=file_id, addParents="root", removeParents=dir_id).execute()
service.files().delete(fileId=dir_id).execute()这将首先将文件移出文件夹(并进入“我的驱动器”),然后删除该文件夹。
注意:如果您调用delete()而不是trash(),文件夹及其内的所有文件都将被永久删除,因此无法恢复它们!所以在对文件夹使用此方法时要非常小心.
https://stackoverflow.com/questions/33692184
复制相似问题