For private repo:
import requests
def get_file_contents(repo_url, file_path, access_token):
# Construct the API URL to access the file content
api_url = f"{repo_url}/contents/{file_path}"
# Send a GET request to the API with the access token
headers = {
"Authorization": f"token {access_token}",
"Accept": "application/vnd.github.v3+json"
}
response = requests.get(api_url, headers=headers)
if response.status_code == 200:
# The response contains the file content in Base64 encoding.
# You may want to decode it to get the actual content.
content = response.json().get("content", "")
decoded_content = base64.b64decode(content).decode("utf-8")
return decoded_content
else:
print(f"Failed to retrieve file. Status Code: {response.status_code}")
return None
if __name__ == "__main__":
# Replace these with your specific repository URL, file path, and access token
repo_url = "https://api.github.com/repos/<username>/<repo>"
file_path = "path/to/your/file.txt"
access_token = "YOUR_PERSONAL_ACCESS_TOKEN"
content = get_file_contents(repo_url, file_path, access_token)
if content:
print(content)
Go to your GitHub account settings.
Navigate to "Developer settings" -> "Personal access tokens."
Click "Generate new token."
Give the token an appropriate name, select the desired permissions (usually, "repo" scope is sufficient for private repository access), and click "Generate token."
Copy the generated token and use it in the access_token variable in the Python program
Public repo:
If you want to access .txt files from a public Github repository without cloning or downloading the files, you can use the Github API to fetch the contents of the files directly. One way to achieve this is by using the requests library in Python. Here's a simple example of how to do it:
import requests
def get_file_contents(repo_url, file_path):
# Construct the API URL to access the file content
api_url = f"{repo_url}/contents/{file_path}"
# Send a GET request to the API
response = requests.get(api_url)
if response.status_code == 200:
# The response contains the file content in Base64 encoding.
# You may want to decode it to get the actual content.
content = response.json().get("content", "")
decoded_content = base64.b64decode(content).decode("utf-8")
return decoded_content
else:
print(f"Failed to retrieve file. Status Code: {response.status_code}")
return None
if __name__ == "__main__":
# Replace these with your specific repository URL and file path
repo_url = "https://api.github.com/repos/<username>/<repo>"
file_path = "path/to/your/file.txt"
content = get_file_contents(repo_url, file_path)
if content:
print(content)
Note: In the repo_url, replace with the owner's GitHub username and with the repository name.
Please ensure you have the requests library installed (pip install requests) before running the above code. Also, remember to replace the placeholders in the repo_url with the appropriate values.
This approach allows you to fetch the contents of the .txt file directly from the public GitHub repository without cloning or downloading the entire repository. However, if the repository becomes private or requires authentication in the future, this method will no longer work.