user11397474
user11397474

Reputation:

Append text to a text file that is on my google drive (Python)

I uploaded text file to my google drive and I want to know how can I append text to that file using python and how can other people that have my exe file of that python file also append it (so it works for other people also). I put that file to have shareable link and with that link you can edit that text file. I tried with urllib.request.urlopen but it says that it is expected str, bytes and not HTTP response. With urlopen i can print what that text file contains but cant write to it for some reason. Also thinking this should be done with pydrive but I don't know how.

Upvotes: 0

Views: 1976

Answers (2)

user11397474
user11397474

Reputation:

This worked thanks to clarification and guidance from Martin Schere and Tanaike, thanks! Appending the text is done with "\n" because I don't know any other way, but it works.

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth=GoogleAuth()
drive=GoogleDrive(gauth)

fileList=drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for files in fileList:
    print('title: %s, id: %s' % (files['title'],files['id']))
    if files['title'] == 'user_info.txt':
        files.GetContentFile("user_info.txt")
        update = files.GetContentString() + "\ntest"
        files.SetContentString(update)
        files.Upload()   
        break

Upvotes: 1

martin
martin

Reputation: 885

Yes, pydrive is the answer. You should authenticate your app and then write the code to automate what you're trying to achieve. Then you should compile everything into an .exe file and you're set. Here's the doc: https://pythonhosted.org/PyDrive/

Upvotes: 0

Related Questions