There are some good solutions already, but if you use requests just follow Github's API.

The endpoint for all content is

GET /repos/:owner/:repo/contents/:path

But keep in mind that the default behavior of Github's API is to encode the content using base64.

In your case you would do the following:

#!/usr/bin/env python3
import base64
import requests


url = 'https://api.github.com/repos/{user}/{repo_name}/contents/{path_to_file}'
req = requests.get(url)
if req.status_code == requests.codes.ok:
    req = req.json()  # the response is a JSON
    # req is now a dict with keys: name, encoding, url, size ...
    # and content. But it is encoded with base64.
    content = base64.b64decode(req['content'])
else:
    print('Content was not found.')
Answer from dasdachs on Stack Overflow
Discussions

python - Read content of file which is in GitHub using PyGitHub - Stack Overflow
Else, do we have some other package to read such file content? ... 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()) More on stackoverflow.com
🌐 stackoverflow.com
A Python script which can fetch the contents of a GitHub repo using it's URL
You definitely do not need any github APIs to do it, to be honest. 2 lines of shell script suffices. 1st line is the curl call 2nd line is the unzlip call. Also, if you do have git installed then git clone apparently magically woks. So op u/Lost_Championship698 what is the purpose of this script? Here is the script for you: curl "https://$REPO/archive/refs/heads/$BRANCH.zip" -o "./$REPO_$BRANCH.zip" More on reddit.com
🌐 r/developersIndia
10
2
August 3, 2024
I created a Python script to view a Github repository's file size!
Very nice! I've often wanted to know the size of a repo without actually cloning it, and I'm usually too lazy to play around with Github's API. The biggest issue with this is that it's completely interactive and not very flexible. Instead of an interactive script, the core utility should be a function (or set of functions) with clearly defined inputs and outputs. It should be up to the user to determine what information they want and how they want to use it. This function should take a repo URL and optionally a branch name, tag name, commit SHA, etc. (since they might want information for a specific commit or branch) as arguments. If the input is invalid or if the Github API request returns an error, it should raise the appropriate exception. Don't assume the user wants the size in MB; just return the raw information in bytes. Return all of the repo information in a dictionary, for example, so the user can grab from it what they need. All of this would allow the user to import the function and use it in their own scripts, if desired. If you wanted to, you could wrap the interactive stuff in a if __name__ == "__main__" block, or better yet, get argparse involved and simply let the user provide arguments to the script in the command line call, and get rid of the interactive stuff entirely. More on reddit.com
🌐 r/learnpython
33
171
August 15, 2019
Python Module that Extracts the Browsers History

I'm a little confused, Should I delete this module from Pypi Index?

More on reddit.com
🌐 r/Python
43
58
August 20, 2018
🌐
Python Forum
python-forum.io › thread-26072.html
Read content of GitHub file
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 t
🌐
Medium
medium.com › towards-entrepreneurship › importing-a-csv-file-from-github-in-a-jupyter-notebook-e2c28e7e74a5
Loading a csv file from GitHub in Python | Towards Entrepreneurship
May 17, 2020 - To avoid any paths problems and have a file readable from any platform, a solution I recommend is to put the .csv file in a GitHub repository and access it directly in the code.
🌐
GitHub
gist.github.com › keithweaver › b0912519d410b7e2ab3c98bf350bcfc2
Get File Content with Python · GitHub
January 6, 2020 - Get File Content with Python · Raw · get-file.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters · Show hidden characters · Copy link · Copy Markdown · two-line way of doing the same thing: from pathlib import Path fileContent = Path('./test.txt').read_text() Sign up for free to join this conversation on GitHub.
🌐
GitHub
gist.github.com › studiawan › 6818882
Read text file using Python · GitHub
Read text file using Python · Raw · read.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·
🌐
Testengineeringnotes
testengineeringnotes.com › posts › 2023-03-25-python-github-api
Get file content from Github repository — Test Engineering Notes
March 25, 2023 - But we care only about content. “Content” is encoded with base64 - so we decode it before the usage. base64.b64decode() method returns result in the binary format. Before using it as JSON string - we need to decode it with utf-8 format: ...
Find elsewhere
🌐
GitHub
github.com › aushafy › Python-Write-and-Read-File
GitHub - aushafy/Python-Write-and-Read-File: This program was created as simulation for the file processing using Python
read-lines-in-a-list.py (simple program to read content of file but stored in a list) write.py (simple program to write some text in an empty file)
Forked by 4 users
Languages   Python 100.0% | Python 100.0%
🌐
GitHub
github.com › simonw › github-contents
GitHub - simonw/github-contents: Python class for reading and writing data to a GitHub repository · GitHub
Python class for reading and writing data to a GitHub repository - simonw/github-contents
Starred by 18 users
Forked by 4 users
Languages   Python
🌐
PyPI
pypi.org › project › github-contents
github-contents · PyPI
June 9, 2019 - Python class for reading and writing data to a GitHub repository
      » pip install github-contents
    
Published   Feb 24, 2021
Version   0.2
🌐
GitHub
gist.github.com › madan712 › f05318a3f95eda0bc40bed795ba9e96c
Python - Read and write a text file · GitHub
Python - Read and write a text file · Raw · read_write_file.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
YouTube
youtube.com › watch
How to read GitHub csv files directly into Python - YouTube
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
Published   September 21, 2020
🌐
GitHub
gist.github.com › macloo › 2a04140900225e9fee012d487280f957
How to open, read, write and close files in Python · GitHub
How to open, read, write and close files in Python · Raw · gettysburg.txt · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
GitHub
gist.github.com › bkamapantula › 1304749
Read file in Python · GitHub
Read file in Python · Raw · readfile.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·
🌐
DEV Community
dev.to › seenevasaraj › read-files-in-git-repo-using-different-modules-in-python-4fj4
READ FILES IN GIT REPO USING DIFFERENT MODULES IN PYTHON - DEV Community
February 26, 2024 - Read files from Git repository using GitPython in Python,which provides high-level interface to interact with Git repositories.
🌐
GitHub
github.com › arshren › Reading-Files › blob › master › Python - Reading Files .ipynb
Reading-Files/Python - Reading Files .ipynb at master · arshren/Reading-Files
"# reading an HTML file\n", "# in the uel the data is stored in a table format\n", "from pandas import read_html\n", "url=\"https://www.fdic.gov/bank/individual/failed/banklist.html\"\n", "bank_data = pd.io.html.read_html(url)\n", "bank_data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "url=\"https://www.cnn.com/2018/09/07/politics/trump-inauguration-photos/index.html\"\n", "import urllib.request\n", "from bs4 import BeautifulSoup\n", "from gensim.summarization.summarizer import summar
Author   arshren
🌐
Medium
medium.com › plumbersofdatascience › import-and-export-files-to-and-from-github-via-api-626efd7dd859
Import and Export Files to and from GitHub via API | by Henry Alpert | Plumbers Of Data Science | Medium
January 12, 2023 - On the left sidebar, there’s ... a variable in the format username/repo-name. Use a with statement with open to read the file contents and assign the contents to a variable....
🌐
Reddit
reddit.com › r/developersindia › a python script which can fetch the contents of a github repo using it's url
r/developersIndia on Reddit: A Python script which can fetch the contents of a GitHub repo using it's URL
August 3, 2024 -

GitHub's API provides basic data about a repository and its contents but does not offer detailed contents of each file unless you specify it in the path while sending the request.

I was working on a project that required extracting code from public repositories using their URLs, and we couldn't find a reliable method to retrieve the repository contents in JSON format.

So i decided to make a script for it. When you run the script it will ask for a url and then it will return the file structure of that repo and the contents in each file. Planning to make it into a package for py and js.

🌐
GitHub
github.com › zairahira › read-files-python
GitHub - zairahira/read-files-python: Sample text file for practice · GitHub
This Python program opens files for reading and writing using file handles.
Forked by 7 users
Languages   Python