如何使用 PyGithub 附加到文件?

How to append to a file using PyGithub?

我想在 Python 中使用 PyGithub 附加到一个已经存在的 .txt 文件。 我试过这段代码:

git = Github(TOKEN)
repo = git.get_repo("Repository")
file = repo.get_contents("Textfile.txt", ref="Ref")

repo.update_file(file.path, "test", "Text I wanna store", file.sha, branch="Ref")

但是这段代码删除了旧数据,只存储了这些数据,即“我想存储的文本”。

我想将此数据存储在先前存储的数据的末尾,例如 “我要存储的以前的数据文本”

上面的代码已经很好了,您只需要获取文件的原始内容,并使用 + 运算符或 f-string

追加新的内容

这是一个例子

git = Github(TOKEN)
repo = git.get_repo("Repository")
file = repo.get_contents("Textfile.txt", ref="Ref")
new_data = input("Text you want to add")
update_file(file.path, "NEW COMMIT", f"{file} {new_data}", file.sha,branch="Ref")