Search code examples
github-apipygithub

Create file in GitHub repository with PyGithub


I have the following code:

import github

token = "my gitHub token"
g = github.Github(token)
new_repo = g.get_user().create_repo("NewMyTestRepo")

print("New repo: ", new_repo)

new_repo.create_file("new_file.txt", "init commit", "file_content ------ ")

I have run this code, and this is the result:

New repo:  Repository(full_name="myname/NewMyTestRepo")
Traceback (most recent call last):
  ...
  File "/home/serega/PycharmProjects/GitProj/myvenv/lib/python3.5/site-packages/github/Requester.py", line 180, in __check
    raise self.__createException(status, responseHeaders, output)
github.GithubException.UnknownObjectException: 404 {'message': 'Not Found', 'documentation_url': 'https://developer.github.com/v3'}

I think there may be problem in the scope of my token, it has repo scope. Nevertheless, I have managed to create a repo, so it seems, it should be allowed to make commit in that repo with new file inside.

About scopes I saw that link : https://developer.github.com/v3/oauth/#scopes

And it states:

repo
Grants read/write access to code, commit statuses, repository invitations, collaborators, and deployment statuses for public and private repositories and organizations.

I will really appreciate if somebody can clarify about required token's scope, and what could be the problem.


Solution

  • repo scope is enough to create files in a repository. It would seem from this question that the problem is that your file must have a leading slash:

    new_repo.create_file("new_file.txt", "init commit", "file_content ------ ")