1

I want to get all files (and sub-directories and the files in them) from a github repo. I do not want to use the git package as that requires me to have git installed (on windows, so that's not an auto thing).

Willing to use urllib or something else instead. I'm on python 2.

I am able to get a single file as from here < How to download and write a file from Github using Requests > with:

filename = getcwd() + '\\somefile.txt'

url = 'https://raw.github.com/kennethreitz/requests/master/README.rst'

r=requests.get(url)

with open(filename,'w+') as f:
    f.write(r.content)

How do I copy the whole repo?

azazelspeaks
  • 5,727
  • 2
  • 22
  • 39
  • "I do not want to use the git package as that requires me to have git installed" Which git package are you referring to? I don't know of a Python package named just `git`, and I'm pretty sure `git-python` has a pure Python implementation of the git protocol and can use the GitDB pure Python implementation of the git repo database. – abarnert Jul 26 '18 at 00:04
  • Also, if you only care about Windows, you could just include PortableGit in your package and run its `git.exe`, without needing to install anything. (Or you can do that for Windows, assume `git` is on the PATH otherwise, which will work on Windows, macOS, and almost every Linux and *BSD in the world…) – abarnert Jul 26 '18 at 00:06

1 Answers1

2

You can download the whole github repo as a .zip file by making a request to the https://github.com/user/repo/archive/branch.zip url. Where branch is the name of the branch you would like to download (usually master).

Example:

import os

filename = os.path.join(os.getcwd(), 'repo.zip')
url = 'https://github.com/requests/requests/archive/master.zip'

r = requests.get(url)

with open(filename, 'wb') as f:
    f.write(r.content)

You also should open the file in binary mode just in case (with wb) because it is saving a .zip file.

Karl
  • 1,664
  • 2
  • 12
  • 19