pythongithubpygithub

PyGitHub script has no errors but does not work


I have two csv files that update on a regular basis that supply data to Render for a dashboard. The entire process is automated except for pushing the new information to github, which I've been doing in the terminal. I need a script that automates this part too.

Using PyGithub documentation and several posts on this site, I've composed the following:

from github import Github

g = Github('token')
repo = g.get_user('name').get_repo('repo')

contents_h = repo.get_contents('src/history.csv')

his = '{full path}/src/history.csv'

with open(his, "r") as file:
    new_h = file.read()

repo.update_file(his, "updating file", new_h, contents_h.sha, branch='main')

It runs without errors, but the repository does not update. I suspect that I misunderstand some attribute, but changes haven't worked either.

What do I need to change?

AFTER EDIT (added , "r"): okay, now I am getting a file "does not match" error from the last line. So, I'm not sure what one of those parameters should be.


Solution

  • Okay, so it was simple once I had an error message. Changed the last line:

    from github import Github
    
    g = Github('token')
    repo = g.get_user('name').get_repo('repo')
    
    contents_h = repo.get_contents('src/history.csv')
    
    his = '{full path}/src/history.csv'
    
    with open(his, "r") as file:
        new_h = file.read()
    
    repo.update_file(contents_h.path, "updating file", new_h, contents_h.sha)