pythongitpygithub

How to create a file inside a repository using PyGithub?


As the documentation suggests, calling create_file on a github.Repository.Repository object should create a file but I am getting github.GithubException.UnknownObjectException.

My code is like this:

`repo.create_file('filename', 'commitmessage', 'content')`

What am I doing wrong?


Solution

  • It seems that you need to start the filename with a slash /. I've tried the following command, and it worked:

    In [12]: repo.create_file('/filename', 'commitmessage', 'content')
    Out[12]:
    {'commit': Commit(sha="201e0e5b91f7ec431d5b06cc47affff202e3de04"),
     'content': ContentFile(path="filename")}