使用python在github上推送压缩文件

时间:2019-05-06 07:48:26

标签: python-3.x github github-api gitpython

我想使用python代码或API在github上推送.zip文件,但是这样做时,我在stackoverflow上发现了一些资源,虽然该文件已被推送,但被推送的数据已损坏,无法恢复。 / p>

尝试过这个, How do I push new files to GitHub?

2 个答案:

答案 0 :(得分:0)

尝试使用git hub API v3将zip文件上传到github

答案 1 :(得分:0)

PyGithub的帮助下,您可以使用以下代码段:

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