Search code examples
pythonpygithub

How to update a file using PyGithub?


I want to know which method should I call (and on which object) and how to call that method (required parameters and their meanings).


Solution

  • 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)
    

    If you are using token then you should have at least repo scope of your token to do it. https://developer.github.com/v3/oauth/#scopes

    See: https://developer.github.com/v3/repos/contents/ and https://github.com/PyGithub/PyGithub