Created
July 10, 2021 02:08
-
-
Save vikramsoni2/029647ec8cbb4a525383e494f3e07531 to your computer and use it in GitHub Desktop.
extract zip file containing github repo witout the top level directory
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
from requests import get | |
from io import BytesIO | |
from zipfile import ZipFile | |
import os | |
from pathlib import Path | |
destination = "testproject" | |
file_url = 'https://github.com/vikramsoni2/aihubcli/archive/refs/heads/main.zip' | |
request = get(file_url) | |
zf = ZipFile(BytesIO(request.content)) | |
def createdirs(filename): | |
if not os.path.exists(os.path.dirname(filename)): | |
try: | |
os.makedirs(os.path.dirname(filename)) | |
except OSError as exc: # Guard against race condition | |
if exc.errno != errno.EEXIST: | |
raise | |
for n in [n for n in zf.namelist() if not n.endswith('/')]: | |
target_file = n.replace(zf.namelist()[0], destination+'/') | |
createdirs(target_file) | |
with open(target_file, "wb") as f: | |
f.write(zf.read(n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment