简体   繁体   中英

Google Drive SDK: I want to update a file, not create new one

In my opinion it should be very easy, but because of some reason I can't find the right way how to do it: how should I update a file in Google Drive using Python?

My code:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LoadCredentialsFile("mycreds.txt")
drive = GoogleDrive(gauth)
file = drive.CreateFile({'title': 'Hello.txt'})
file.SetContentString('test 1')
file.Upload()

This creates a new file. Now I want to add to this file next line 'test 2'. Running above code each time creates new file, which is not what I want.

Can anyone help me with this please?

Daddy

This is because you call CreateFile every time you run the script and therefore creating a new document.

If you'd want to update the file without closing the script:

file = drive.CreateFile({'title':'appdata.json', 'mimeType':'application/json'})
file.SetContentString('{"firstname": "John", "lastname": "Smith"}')
file.Upload() # Upload file
file.SetContentString('{"firstname": "Claudio", "lastname": "Afshar"}')
file.Upload() # Update content of the file

I haven't found a way to get a GoogleDriveFile instance by ID, but the documentation mentions iterating over all files that match a description:

file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file in file_list:
  print 'title: %s, id: %s' % (file['title'], file['id'])

So if you use this by searching for your file and checking if the list contains only one item, you'd have found your specific document. For search parameters of 'q': https://developers.google.com/drive/web/search-parameters .

file_list = drive.ListFile({'q': "title='hello.doc' and trashed=false"}).GetList()
if len(file_list) == 1:
    file = file_list.next()
updated_content = file.GetContentString() + "New content"
file.SetContentString(updated_content)
file.Upload()

Sorry I don't know more details, if this doesn't work for you maybe look at the official python Google API: https://developers.google.com/drive/web/quickstart/python

First of all, each time you use drive.CreateFile() and file.Upload() , it will create a new instance. To overwrite on the same file, you have to specify the file id of that file. For example, you may create a new file like this:

yourfile_id = '*****'
file = drive.CreateFile({'title': 'Hello.txt', 'id': yourfile_id})

This way, you will make sure that you would not have a duplication of the file.

Second, to update your file, you have to read it and append what you want to add to the read data. Below, show an example:

file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()

for file in file_list:
    if file['title'] == 'Hello.txt':
        new_file = drive.CreateFile({'title': file['title'], 'id': file['id']})
        file_content = new_file.GetContentString()
        file_content = file_content + '\ntest 2'
        new_file.SetContentString(file_content)
        new_file.Upload()

the first line gets a list of all files in the root (you can search in any subfolders by replacing the 'root' with the folder id) The for loop finds the file you want ('Hello.txt') and feed the new_file with its title and id (to replace with the old one, as the piece of code mentioned before) Next two lines read the file content and append new data and the last two lines upload and update the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM