Skip to content

Instantly share code, notes, and snippets.

@niko86
Last active April 9, 2016 14:30
Show Gist options
  • Save niko86/4cc80e702f60475ee3ade91e6b20403d to your computer and use it in GitHub Desktop.
Save niko86/4cc80e702f60475ee3ade91e6b20403d to your computer and use it in GitHub Desktop.
Extracts all .shp files from any .zip files in the current working directory of the script.
#! python3
import os
import shutil
import zipfile
from zipfile import ZipFile
from os.path import basename
list_zip_file = [files for files in os.listdir(".") if zipfile.is_zipfile(files)]
for zip_file in list_zip_file:
zip_file = ZipFile(zip_file)
list_shp_files = [member.filename for member in zip_file.infolist() if member.filename.endswith('.shp')]
for shp_file in list_shp_files:
source = ZipFile.open(zip_file, name=shp_file, mode='r')
target = basename(shp_file)
shutil.copyfileobj(source, open(target, mode='wb'))
@niko86
Copy link
Author

niko86 commented Apr 8, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment