UPDATE: added check to loop to avoid trying to write the zipfile into itself and causing an infinite loop (!) (discussed downthread)
UPDATE: this function requires the two inputs folder (path to the shapefile) and name (shapefilename, for example if your shapefile is foo.shp, the name should be foo (without the extension)
import os
import glob
import zipfile
def zipshape(folder, name):
os.chdir(folder)
zipname = name + ".zip"
with zipfile.ZipFile(zipname, "w") as newzip:
for ff in glob.glob(name + ".*"):
if ff[-4:] != "lock" and ff != zipname:
newzip.write(ff)
return os.path.join(folder,zipname)