Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Download a file from a GitHub repository using Python

I would like to download a single file from a GitHub repository. In bash, you could do something like this:

curl -kLSs "https://github.com/mikolalysenko/lena/archive/master.tar.gz" | tar xz --wildcards '*lena.png' --strip-components=1`

to download and save this file in the current working directory. How would one do this using only Python (aka. not calling a bash command)?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I am not sure if with "pure python" you mean without modules, if not using the method urlretrive from the urllib module could be a solution:

import urllib.request


def main():
    urllib.request.urlretrieve("https://raw.githubusercontent.com/mikolalysenko/lena/master/lena.png", "test.png")


if __name__ == "__main__":
    main()

To download files from github you have to use the raw.githubusercontent.com domain, because the files of github repositories get stored there, but this answer explains it better.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading