0

I'm trying to delete an existing excel file so that I can upload the newest version. The file is in a shared folder and thus the struggle, I could not really find anything on the deleting of a file in a shared folder. Below is the piece of code that is producing an error.

def deleteExistingFile(team_drive_iddel, parent_folder_iddel):
    
    file_name = 'To_Label.xlsx'
    
    fileDel = drive.CreateFile({
    'title': file_name,
    'parents': [{
        'kind': 'drive#fileLink',
        'teamDriveId': team_drive_iddel,
        'id': parent_folder_iddel
    }]
    })
    
    fileDel.Trash()

team_drive_id = '0AHPtZymJwYgsUk9PVA'
parent_folder_id = '1KM6UIvmJ1nSZa62yFUxoQ-L4q6JZb4_W'
deleteExistingFile(team_drive_id,parent_folder_id)`

Any help would be appreciated.

The above is what I tried and it produced the error, I'm also not sure where the "file_id" might be needed.

Pawel Kam
  • 1,684
  • 3
  • 14
  • 30
BrianTM
  • 11
  • 1

1 Answers1

1

I found this answer on here(stack overflow) but it had no upvotes and now I just can't find it anymore.

The solution was switching from pydrive to pydrive2, so you would have:

from pydrive2.auth import GoogleAuth  
from pydrive2.drive import GoogleDrive

gauth = GoogleAuth()  
gauth.LoadClientConfigFile('client_secrets.json')  
drive = GoogleDrive(gauth)

file = drive.CreateFile({'id': 'YOURfileID'})  
file.Trash()

try not use the file.Delete() in case you permanently delete something you did not want to delete, like how I deleted a whole folder :) Be safe and use file.Trash()

BrianTM
  • 11
  • 1