OverWrite files on upload
Hi, is there any way to tell PyDrive to overwrite existing files on upload?
If you specify the same id, you then SetContentFile() and Upload() it overwrites the file's content.
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
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()
-
Copy the folder id Manually from the web-page address which shows up when you click on the folder to get inside it

-
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()
- 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']
- Update Existing File
file1 = drive.CreateFile({'id':id})
file1.SetContentFile('file_name')
file1.Upload()
1. Copy the folder id Manually from the web-page address which shows up when you click on the folder to get inside it  2. Insert the Folder_id in following code to get all files list in the above folderfile_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 Filefile1 = 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()
I think instead of upload, just go ahead and use file.patch() if I am correct?
@himanshugullaiya Answer is it :)