0

I have a CSV file with one column. This column consists of around 100 GitHub public repo addresses (for example, NCIP/c3pr-docs)

I want to know if there is any way to download all these 100 public repo inside my computer using Python. I don't want to use any command on the terminal, I need a function for it.

I use a very simple code to access to user and repo. Here it is:

import csv
import requests
#replace the name with your actual csv file name
file_name = "dataset.csv"
f = open(file_name)
csv_file = csv.reader(f)
second_column = [] #empty list to store second column values
for line in csv_file:
    if line[1] == "Java":
     second_column.append(line[0])
     print(line[0]) #index 1 for second column

So by doing this I read a CSV file and get access to the users and repo. I need a piece of code to help me download all these repo

1 Answer 1

1

Try this:

import requests

def download(user_and_repo, branch):
    URL = f"https://github.com/{user_and_repo}/archive/{branch}.tar.gz"
    response = requests.get(URL)
    open(f"{user_and_repo.split('/')[1]}.tar.gz", "wb").write(response.content)

download("AmazingRise/hugo-theme-diary", "main")

Tested under Python 3.9.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your response, import csv import requests #replace the name with your actual csv file name file_name = "dataset.csv" f = open(file_name) csv_file = csv.reader(f) second_column = [] #empty list to store second column values for line in csv_file: if line[1] == "Java": second_column.append(line[0]) print(line[0]) #index 1 for second column This is my code and I just have user and repo like NCIP/c3pr-docs in such case how can I download these repo?
@ZBokaee I've modified the answer. Just pass that NCIP/c3pr-docs into the function download.
Thank you very much. Shouldn't repo in this line open(f"{repo}.tar.gz", "wb").write(response.content) be changed to user_and_repo, is so my program shows me this error: FileNotFoundError: [Errno 2] No such file or directory: 'AmazingRise/hugo-theme-diary.tar.gz'
do you have any idea about that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.