Search code examples
python-3.xgithubgithub-apigitpython

pushing zipped files on github using python


i want to push a .zip file on github using python code or API, but in doing so with some resources which i found on stackoverflow, the file is being pushed but the data pushed is corrupted and cannot be retrieved back.

Tried this, How do I push new files to GitHub?


Solution

  • with the help of PyGithub , You Can Use this Snippet:

    import base64
    now = datetime.datetime.now()
    # read binary file and convert it to ascii code using base64 library
    data = base64.b64encode(open(file_name, "rb").read())
    path = "{}/{}/{}".format(now.year, now.month, "tweets.zip")
    # pygithub needs string for creating blob so we decode binary data to utf-8 to be parsed correctly
    blob = repo.create_git_blob(data.decode("utf-8"), "base64")
    element = InputGitTreeElement(path=path, mode='100644', type='blob', sha=blob.sha)
    element_list.append(element)
    tree = repo.create_git_tree(element_list, base_tree)
    parent = repo.get_git_commit(master_sha)
    commit = repo.create_git_commit("commit_message", tree, [parent])
    master_ref.edit(commit.sha)```
    
    
      [1]: https://github.com/PyGithub/PyGithub