将文本附加到我的Google驱动器(Python)上的文本文件中

问题描述 投票:0回答:2

我已将文本文件上传到我的Google驱动器,我想知道如何使用python将文本附加到该文件,以及拥有该python文件的exe文件的其他人也如何附加该文件(因此它也适用于其他人)。我将该文件设置为可共享的链接,并可以使用该链接编辑该文本文件。我尝试使用urllib.request.urlopen,但是它说应该是str,字节而不是HTTP响应。使用urlopen,我可以打印该文本文件包含的内容,但由于某种原因无法写入该文件。也认为应该使用pydrive完成此操作,但我不知道如何。

python python-3.x google-drive-api text-files
2个回答
0
投票

是的,pydrive是答案。您应该对您的应用进行身份验证,然后编写代码以自动化您要实现的目标。然后,应将所有内容编译为.exe文件并进行设置。这是文档:https://pythonhosted.org/PyDrive/


0
投票

这要感谢Martin Schere和Tanaike的澄清和指导,谢谢!附加文本用“ \ n”完成,因为我不知道其他任何方法,但是可以。

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
© www.soinside.com 2019 - 2024. All rights reserved.