Zip all shapefiles in directory individually

Posted by in ArcMap, Data, GIS

This script zips individual shapefiles within a defined directory. The zipfiles are named identical to the shapefile itself. This is useful for platforms like CartoDB and MapBox, especially if you are in an Esri workflow. If you are usging a geodatabase, it can be converted to shapefiles, then those shapefiles can be passed through this script and easily uploaded to either web platform. I commented this one very well, which I normally slack on, basically just change the source and destination folder on lines 11 and 12. If the destination doesn’t exist, it will be created by the script.

Here’s a sample. This is my folder of shapefiles.

01
After the script is run, the folder “zip” is created.

02
Inside the “zip” folder, each shapefile is zipped into it’s own zipfile with the same name as the shapefile itself.

03

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# brian greer
#4/2015
#directory of shapefiles to zip for zipping individual shapefiles
 
#import modules needed
import os
import glob
from zipfile import *
 
#define location of shapefiles and destination of zipped shapefiles
source = r"D:\ZipShp"
dest = r"D:\ZipShp\zip"
 
#change the current directory
os.chdir(source)
 
#test current directory
retval = os.getcwd()
print retval
 
#list all files with extension .shp
shps = glob.glob(source+"/*.shp")
print shps
 
# create empty list for zipfile names
ziplist = []
 
# create destination directory if it does not exist
if not os.path.exists(dest):
    os.makedirs(dest)
 
#populate ziplist list of unique shapefile root names by finding all files with .shp extension and removing extension
for name in shps:
  #prints full path for each shapefile
  print name
  #retrieves just the files name for each name in shps
  file = os.path.basename(name)
  #removes .shp extension
  names = file[:-4]
  #adds each shapefile name to ziplist list
  ziplist.append(names)
 
#prints ziplist to confirm shapefile root names have been added
print ziplist
 
#creates zipefiles in dest folder with basenames
for f in ziplist:
  # prints each itme in the ziplist
  print f
  #creates the name for each zipefile based on shapefile root names
  file_name = os.path.join(dest, f+".zip")
  #print to confirm
  print file_name
  #created the zipfiles with names defined above
  zips = ZipFile(file_name, "w")
  print zips
  #files lists all files with the current basename (f) from ziplist
  files = glob.glob(str(f)+".*")
  # iterate through each basename and add all shapefile components to the zipefile
  for s in files:
    print s
    zips.write(s)
  zips.close()

5 Comments

  1. Amber
    January 17, 2017

    This is amazing!!!! Works! We are batch zipping .sid imagery files!
    Thank you a million!

    Reply
  2. Langdon
    June 19, 2018

    Great script again, one important note, make sure your destination folder is somewhere other than your input folder. Otherwise I made an infinite loop that kept dumping in the inputs continuously somewhere along:

    files = glob.glob(str(f)+”.*”)
    # iterate through each basename and add all shapefile components to the zipefile
    for s in files:
    print s
    zips.write(s)

    Huge help though!

    Reply
  3. James
    November 8, 2018

    Brian, thanks very much for sharing your script. It worked perfectly for me and saved me having to muddle through writing something like this myself. Kudos!

    Reply
  4. Riad hamoudi
    April 25, 2019

    Great job! work well for me!
    do you have idea how I can give hand to usesr to change the source and dest dir,
    Thanks
    source = r”D:\ZipShp” dest = r”D:\ZipShp\zip”

    Reply
  5. Dale Watt
    February 25, 2021

    Man, this worked like a charm! Thank you so much for the great script.

    Reply

Leave a Reply