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.
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()