PyDrive icon indicating copy to clipboard operation
PyDrive copied to clipboard

OverWrite files on upload

Open Kameecoding opened this issue 8 years ago • 7 comments

Hi, is there any way to tell PyDrive to overwrite existing files on upload?

Kameecoding avatar Nov 09 '17 10:11 Kameecoding

If you specify the same id, you then SetContentFile() and Upload() it overwrites the file's content.

RNabel avatar Nov 09 '17 11:11 RNabel

Well since I could not get the id replaced on new files.... Heres another way. Getting Files of the same name. Deleting the old one and then uploading the new one. [ if title is the same. Delete old file. Upload new one. else upload the file as is ]

def upload_to_google_drive(file):
settings_path = 'settings.yaml'
gauth = GoogleAuth(settings_file=settings_path)
drive = GoogleDrive(gauth)
file_list = drive.ListFile({'q': "'root' in parents"}).GetList()
try:
        for file1 in file_list:
            if file1['title'] == file:
                file1.Delete()
                f = drive.CreateFile()
                f.SetContentFile(file)
                f.Upload()
            else:
                f = drive.CreateFile()
                f.SetContentFile(file)
                f.Upload()
    except:
        pass

MateoWartelle avatar Dec 09 '17 22:12 MateoWartelle

FYI, for anyone else doing the same as the above but doing so in a subfolder of Google drive, here's the version that works for that. Basically the same as the above but with an extra filter for subfolder and to filter out any trashed versions of the file. Figured I'd save the next person the work. The below only requires that the googlefolderidstring piece be replaced with your actual id string.

def upload_to_google_drive(file):
settings_path = 'settings.yaml'
gauth = GoogleAuth(settings_file=settings_path)
drive = GoogleDrive(gauth)
file_list = drive.ListFile({'q':"'googlefolderidstring' in parents and trashed=False"}).GetList()
    try:
        for file1 in file_list:
            if file1['title'] == file:
                file1.Delete()                
    except:
        pass
f = drive.CreateFile()
f.SetContentFile(file)
f.Upload()

nataelj avatar Feb 26 '19 14:02 nataelj

  1. Copy the folder id Manually from the web-page address which shows up when you click on the folder to get inside it image

  2. Insert the Folder_id in following code to get all files list in the above folder

 file_list = drive.ListFile({'q':"'14X-yF5v5eFOlRTMLbQEY5WCkgc-m4gtO'  in parents and 
 trashed=False"}).GetList()
  1. find your file by title & return it's ID:
   for x in range(len(file_list)):
         if file_list[x]['title'] == file_name:
               return file_list[x]['id']

  1. Update Existing File
file1 = drive.CreateFile({'id':id})
file1.SetContentFile('file_name')
file1.Upload()

himanshugullaiya avatar Nov 27 '19 05:11 himanshugullaiya

1. Copy the folder id Manually from the web-page address which shows up when you click on the folder to get inside it
   ![image](https://user-images.githubusercontent.com/54324908/69694959-9e327300-1100-11ea-8fad-e22c13b10ea1.png)

2. Insert the Folder_id in following code to get all files list in the above folder
 file_list = drive.ListFile({'q':"'14X-yF5v5eFOlRTMLbQEY5WCkgc-m4gtO'  in parents and 
 trashed=False"}).GetList()
1. find your file by title & return it's ID:

for x in range(len(file_list)): if file_list[x]['title'] == file_name: return file_list[x]['id']


    1. Update Existing File


file1 = drive.CreateFile({'id':id}) file1.SetContentFile('file_name') file1.Upload()

This is the best solution I've found, thanks! Needed to make one minor change to make it work: change return file_list[x]['id'] to file_id = file_list[x]['id']

and, accordingly: file1 = drive.CreateFile({'id':id}) to file1 = drive.CreateFile({'id':file_id})

So the final code looks like this:

file_list = drive.ListFile({'q':"'14X-yF5v5eFOlRTMLbQEY5WCkgc-m4gtO'  in parents and  trashed=False"}).GetList()

for x in range(len(file_list)):
    if file_list[x]['title'] == file_name:
        file_id = file_list[x]['id']

file1 = drive.CreateFile({'id':file_id})
file1.SetContentFile('file_name')
file1.Upload()

elveos avatar Aug 18 '20 13:08 elveos

I think instead of upload, just go ahead and use file.patch() if I am correct?

UBISOFT-1 avatar Jun 30 '21 21:06 UBISOFT-1

@himanshugullaiya Answer is it :)

UBISOFT-1 avatar Jul 04 '21 08:07 UBISOFT-1