I´m writing a small script in python 3.5.2 with pydrive 1.2.1 and I need to be able to delete a file which is not stored locally in my computer, but in my account of Google Drive. The docs only show how to delete a file you previously created, not one which is already stored on the drive. Is deleting an existing file actually possible with pydrive? if so, how?
Asked
Active
Viewed 5,824 times
1 Answers
11
From the docs seems that drive.CreateFile()
create only a reference to a file, this can be local or remote.
As you can se here drive.CreateFile()
is used to download a remote file.
I believe that something like this should do the job:
# Initialize GoogleDriveFile instance with file id.
file1 = drive.CreateFile({'id': <file-id>})
file1.Trash() # Move file to trash.
file1.UnTrash() # Move file out of trash.
file1.Delete() # Permanently delete the file.

Ceppo93
- 1,026
- 10
- 11
-
4Just to confirm `CreateFile()` creates an instance of GoogleDriveFile which can represent an already existing file (i.e. doesn't necessarily create a new file). You can have a look at the source here: https://github.com/googledrive/PyDrive/blob/d4a00a9d11fb4aca10126552ea1cf434522e4d30/pydrive/drive.py#L19 – Robin Nabel Sep 11 '16 at 07:11