Python Forum

Full Version: Read content of GitHub file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Let say , I have repository called "Sample" in my GitHub and inside my repository i have a file named "demo.txt" or any format file.
How do I read the content of file which is in GitHub ?
Can i able to get the content from GitHub API , if it's so , what should i do to get my file content from GitHub using Python ?

I tried this following code but this doesn't work.

from github import Github
g = Github("<Access Token>")
repo = g.get_user().get_repo("sample")
a = repo.get_contents("demo.txt")
print(a.text)
you can interact directly with GitHub API
or you can use package like PyGitHub

Here is example with PyGitHub


from github import Github
g = Github("<Access Token>")
repo = g.get_user().get_repo("sample")
a = repo.get_contents("demo.txt")
print(a.content)
Thanks @buran
But I'm getting encoded output. Actually I need original content.

Is there any possible way to decode it and get original content ?

I can interact directly with GitHub API but i need to transform the data from Github file . It would be good if i approach via PyGithub or any other Python GIT integration.
did you check the docs?
https://pygithub.readthedocs.io/en/lates...ontentfile
try decoded_content instead of content
It works .

Thanks a lot @buran
It worked for my personal GitHub , Thanks @buran
But when i tried for Organizational Github ,I couldn't able to get repo and file content.

from github import Github
g = Github(base_url="organization.github.com", login_or_token="access_token")
repos = g.get_user().get_repo("codetest")
a = repos.get_contents("new.txt")
print(a.content)