[英]pydrive: trying to upload files to Google Drive from a remote server
[英]Delete google drive files by extension with PyDrive
我正在尝试从谷歌驱动器文件夹中删除所有扩展名为“.pdf”的文件。 API 身份验证一切正常,我可以上传文件。 问题是删除。
我在这里上传
upload_file = 'Test1.pdf'
gfile = drive.CreateFile({'parents': [{'id': '11SsSKYEATgn_VWzSb-8RjRL-VoIxvamC'}]})
gfile.SetContentFile(upload_file)
gfile.Upload()
在这里我尝试删除
delfile = drive.CreateFile({'parents': [{'id': '11SsSKYEATgn_VWzSb-8RjRL-VoIxvamC'}]})
filedel = "*.pdf"
delfile.SetContentFile(filedel)
delfile.Delete()
错误:
Traceback (most recent call last):
File "C:/Users/linol/Documents/ProjetoRPA-Python/RPA-TESTE.py", line 40, in <module>
delfile.SetContentFile(filedel)
File "C:\Users\linol\Documents\ProjetoRPA-Python\venv\lib\site-packages\pydrive\files.py", line 175, in SetContentFile
self.content = open(filename, 'rb')
OSError: [Errno 22] Invalid argument: '*.pdf'
我相信你的目标和你目前的情况如下。
在这种情况下,我想提出以下流程。
当上述流程反映到脚本时,它变成如下。
请将###
修改为您的文件夹ID。
# 1. Retrieve file list of PDF file from the specific folder.
fileList = drive.ListFile({'q': "'###' in parents and mimeType='application/pdf'"}).GetList()
# 2. Delete the files using the file list.
for e in fileList:
drive.CreateFile({'id': e['id']}).Trash()
# drive.CreateFile({'id': e['id']}).Delete() # When you use this, the files are completely deleted. Please be careful this.
fileList = drive.ListFile({'q': "'###' in parents and title contains '.pdf'"}).GetList()
。Delete()
时,文件将从 Google Drive 中完全删除。 因此,首先,我想建议使用Trash()
而不是Delete()
作为脚本测试。 这样,文件不会被删除并移动到垃圾箱。 通过这个,我认为你可以测试脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.