AnsweredAssumed Answered

Zip File Script   &   if __name__ == '__main__':

Question asked by jared_pilbeam on Nov 17, 2016
Latest reply on Nov 22, 2016 by Dan_Patterson
#11/17/2016
#
#take all of the shapefiles in a directory and compress them into individual zip files
#
#########################################################################################

#import workspace

import arcpy, os
from arcpy import env
from os import path as p
import RemoveAddLayer
import zipfile
arcpy.overwriteOutput = True


def ZipShapes(path, outpath):
    arcpy.env.workspace = path
    shapes = arcpy.ListFeatureClasses()

#iterate through list of shapefiles
    for shape in shapes:
        name = p.splitext(shape)[0]
        print name
        zip_path = p.join(out_path, name + '.zip')
        zip = zipfile.ZipFile(zip_path, 'w', compression=zipfile.ZIP_DEFLATED)
        zip.write(p.join(path,shape), shape)
        for f in arcpy.ListFiles('%s*' %name):
            if not f.endswith('.shp'):
                zip.write(p.join(path,f),f)
        print 'All file written to %s' %zip_path
        zip.close()


if __name__ == '__main__':

    path = r"Z:\Jared\Python Scripts"
    outpath = r"W:\Data"

    ZipShapes(path, outpath)
       

 

Only a novice at Python. Using ArcMap for Desktop 10.3. My ultimate objective is to send some shapefiles to a zip file using script. I ran the above based on someone's existing script.

1.) What is if __name__ == '__main__': doing in this scenario?

      From what I understand you can use this when importing another Python script? That's why I have the       "RemoveAddLayer" script as an import at the top. I may be completely wrong?

2.) The RemoveAddLayer script (that's imported at the top) is what I created to update Streets and Address Points layers. Should I save these to a folder in the script and then set that as the path variable in the ZipFiles2 script?

 

There was no error when I ran this but also no file was put into the outpath folder. 

 

Edit: @Rebecca: Thanks, I took your advice and put the script in instead of the image. 

Outcomes