1

I am trying to upload a .csv file to GitHub using this code:

### 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')

This is the error I get:

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

I would be fine using any method that might work better. Searching seems to say that this may be an authentication issues [1].

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

1 Answer 1

3

I had the same issue. It worked after I changed the branch='master' by branch='main'.

That's because of that: https://www.zdnet.com/article/github-to-replace-master-with-main-starting-next-month/

Cheers,

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.