5

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

15

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))
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andre Lopez
  • 166
  • 1
  • 4
0

You can use the get_contents API

Here is an example:

from github import Github

github = Github('user', 'password')
user = github.get_user()
repository = user.get_repo('Demo')

file_content = repository.get_contents('sample.txt')
rdas
  • 20,604
  • 6
  • 33
  • 46