Lino Costa
Lino Costa

Reputation: 57

Delete google drive files by extension with PyDrive

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

Answers (1)

Tanaike
Tanaike

Reputation: 201378

I believe your goal and your current situation as follows.

  • You want to delete the files of PDF file in the specific folder.
  • You want to achieve this using pydrive for python.
  • You have already been able to get and put values for Google Drive using Drive API.

In this case, I would like to propose the following flow.

  1. Retrieve file list of PDF file from the specific folder.
  2. Delete the files using the file list.

When above flow is reflected to the script, it becomes as follows.

Sample script:

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.
  • This sample script retrieves the files using the mimeType. When you want to retrieve the files using the filename, you can also use fileList = drive.ListFile({'q': "'###' in parents and title contains '.pdf'"}).GetList().
  • IMPORTANT: In this sample script, when 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.

Note:

  • It seems that PyDrive uses Drive API v2. Please be careful this.

Reference:

Upvotes: 3

Related Questions