Search code examples
pythongithubreadfilepygithub

Read content of file which is in GitHub using PyGitHub


Let’s say "sample.txt" file is in GitHub under the "Demo" repository [Demo/sample.txt]. How can I read the content of sample.txt using PyGitHub instead fetching from the API?

Else, do we have some other package to read such file content?


Solution

  • You can use this code to see the content of the file:

    from github import Github
    github = Github('user', 'password')
    user = github.get_user()
    repository = user.get_repo('Demo')
    file_content = repository.get_contents('sample.txt')
    print(file_content.decoded_content.decode())
    

    If you need to see more attributes like decoded_content, just type this:

    print(help(file_content))