我想在 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")
但此代碼洗掉舊資料并僅存盤此資料,即“我想存盤的文本”
我想將此資料與以前的存盤日期連續存盤,例如“我要存盤的以前的資料文本”
抱歉英語不好
uj5u.com熱心網友回復:
您上面的代碼已經很好了,您只需要獲取檔案的原始內容,并使用 運算子或 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")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/458908.html
