Created
May 17, 2021 04:59
-
-
Save baradhiren/0c2f8763ea89219a4850989600702e66 to your computer and use it in GitHub Desktop.
Extract directory from a zip file Python
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
def extractDirectory(zipName, directoryName, destFilePath): | |
try: | |
with zipfile.ZipFile(zipName) as z: | |
for file in z.namelist(): | |
if file.startswith(directoryName) and file != directoryName + '/': | |
path = os.path.join(destFilePath, 'Any/subsequent/dirs') | |
dirtemp = '/'.join(path.split('/')[0:-1]) | |
Path(dirtemp).mkdir(parents=True, exist_ok=True) | |
with z.open(file) as zf, open(path, 'wb') as f: | |
shutil.copyfileobj(zf, f) | |
except Exception as e: | |
print('[ERROR] Could not extract {}.'.format(directoryName)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment