Reputation: 57
I'm trying to delete all files with the extension '.pdf' from a google drive folder. Everything is fine with the API authentication, I can upload the files. The problem is being the delete.
Here I upload
upload_file = 'Test1.pdf'
gfile = drive.CreateFile({'parents': [{'id': '11SsSKYEATgn_VWzSb-8RjRL-VoIxvamC'}]})
gfile.SetContentFile(upload_file)
gfile.Upload()
Here I try to delete
delfile = drive.CreateFile({'parents': [{'id': '11SsSKYEATgn_VWzSb-8RjRL-VoIxvamC'}]})
filedel = "*.pdf"
delfile.SetContentFile(filedel)
delfile.Delete()
Error:
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'
Upvotes: 2
Views: 1509
Reputation: 201378
I believe your goal and your current situation as follows.
In this case, I would like to propose the following flow.
When above flow is reflected to the script, it becomes as follows.
Please modify ###
to your folder 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()
is used, the files are completely deleted from Google Drive. So at first, I would like to recommend to use Trash()
instead of Delete()
as a test of script. By this, the files are not deleted and moved to the trash box. By this, I thought that you can test the script.Upvotes: 3