使用PyGitHub使用Python将文件推送到GitHub,出现“未找到分支主机”错误

2024-09-19 23:40:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用以下代码将.csv文件上载到GitHub:

### THIS PART WOKRS ###
g = Github("My_Access_Key")
repo = g.get_user().get_repo('tradr')
all_files = []
contents = repo.get_contents("")
while contents:
    file_content = contents.pop(0)
    if file_content.type == "dir":
        contents.extend(repo.get_contents(file_content.path))
    else:
        file = file_content
        all_files.append(str(file).replace('ContentFile(path="','').replace('")',''))
with open('./Data/PriceGrabber/PriceData.csv', 'r') as file:
    content = file.read()
# Upload to github
git_prefix = 'tradr/'
git_file = git_prefix + 'PriceData.csv'
### THIS PART WORKS ###

### GET AN ERROR HERE BELOW ###
if git_file in all_files:
    contents = repo.get_contents(git_file)
    repo.update_file(contents.path, "committing files", content, contents.sha, branch="master")
    print(git_file + ' UPDATED')
else:
    repo.create_file(git_file, "committing files", content, branch="master")
    print(git_file + ' CREATED')

这是我得到的错误:

GithubException: 404 {"message": "Branch master not found", "documentation_url": "https://docs.github.com/rest/reference/repos#create-or-update-file-contents"}

我可以使用任何可能更好的方法。搜索似乎表明这可能是一个身份验证问题[1]

[1]https://github.com/octokit/rest.js/issues/560


Tags: csvpathgitgithubmastergetcontentsrepo