【问题标题】:How to update a file using PyGithub?如何使用 PyGithub 更新文件?
【发布时间】:2016-11-16 11:19:28
【问题描述】:

我想知道我应该调用哪个方法(以及在哪个对象上)以及如何调用该方法(所需参数及其含义)。

【问题讨论】:

标签: python pygithub


【解决方案1】:
import github

g = github.Github(token)
# or  g = github.Github(login, password)

repo = g.get_user().get_repo("repo_name")
file = repo.get_file_contents("/your_file.txt")

# update
repo.update_file("/your_file.txt", "your_commit_message", "your_new_file_content", file.sha)

如果您使用的是令牌,那么您至少应该有 repo 范围 你的令牌来做到这一点。 https://developer.github.com/v3/oauth/#scopes

参见: https://developer.github.com/v3/repos/contents/https://github.com/PyGithub/PyGithub

【讨论】:

    【解决方案2】:

    截至 2021 年,PyGithub API 发生了变化,有一个如何做到这一点的示例:https://pygithub.readthedocs.io/en/latest/examples/Repository.html#update-a-file-in-the-repository

    repo = g.get_repo("PyGithub/PyGithub")
    contents = repo.get_contents("test.txt", ref="test")
    repo.update_file(contents.path, "more tests", "more tests", contents.sha, branch="test")
    # {'commit': Commit(sha="b06e05400afd6baee13fff74e38553d135dca7dc"), 'content': ContentFile(path="test.txt")}
    

    对于.update_file,第一个字符串是消息,第二个字符串是文件的新内容。这是API description

    update_file(path, message, content, sha, branch=NotSet, committer=NotSet, author=NotSet)
    

    【讨论】:

    • 谢谢。请注意,唯一实际的区别似乎是 get_file_contentsget_contents
    猜你喜欢
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 2022-10-12
    • 2015-04-24
    • 2020-07-04
    • 2020-08-01
    • 2018-08-28
    • 1970-01-01
    相关资源
    最近更新 更多