AnsweredAssumed Answered

overwrite output not overwriting?

Question asked by jared_pilbeam on Jul 5, 2017
Latest reply on Jul 7, 2017 by blake.terhune

I made the attached script here into a tool (ArcMap 10.4). When running it the append2Zip() function starting on line 50 appends the txt file to a zipfile as it should, but doesn't overwrite the old one. I run this tool weekly. The other two blocks run as they should (i.e. output overwrites old files). The first block is lines 9-32. The second is 34-48. And the one I'm concerned with is lines 50-60.

 

Can anyone catch why the append2Zip function isn't overwriting?

 

import arcpy
import os
import zipfile
from arcpy import env
from os import path as P

arcpy.env.overwriteOutput = True

#Variables for first block of code
ws = r'\\gisfile\GISstaff\Jared\Python Scripts\Data'
mxd = arcpy.mapping.MapDocument(r'\\gisfile\GISstaff\Jared\Python Scripts\Data\Data_Request.mxd')
addy = arcpy.mapping.Layer(r"Database Connections\ims to plainfield.sde\gisedit.DBO.MGU_Will\gisedit.DBO.Address_Points")
street = arcpy.mapping.Layer(r"Database Connections\ims to plainfield.sde\gisedit.DBO.MGU_Will\gisedit.DBO.Street")
outFeature1 = "WillCounty_AddressPoints"
outFeature2 = "WillCounty_Streets"
                            
# reference dataframe, then remove/add layers in MXD, then save in Data folder
for df in arcpy.mapping.ListDataFrames(mxd):
    for lyr in arcpy.mapping.ListLayers(mxd, "gisedit.DBO.Address_Points", df):
        arcpy.mapping.RemoveLayer(df, lyr) # removes old Adress_Points file from MXD.
        print "removed: {}".format(lyr)
        arcpy.mapping.AddLayer(df, addy, "TOP") # adds new Adress_Points file to MXD fresh from SDE.
        print "added: {}".format(addy)
        arcpy.FeatureClassToFeatureClass_conversion(addy, ws, outFeature1) # converts feature to shapefile and saves in data folder.
        print "saved in data folder: {}".format(outFeature1)
        for lyr in arcpy.mapping.ListLayers(mxd, "gisedit.DBO.Street", df):
            arcpy.mapping.RemoveLayer(df, lyr) # removes old Streets file from MXD.
            print "removed: {}".format(lyr)
            arcpy.mapping.AddLayer(df, street, "TOP") # adds new Streets file to MXD fresh from SDE.
            print "added: {}".format(street)
            arcpy.FeatureClassToFeatureClass_conversion(street, ws, outFeature2) # converts feature to shapefile and saves in data folder.
            print "saved in data folder: {}".format(outFeature2)

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

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

def append2Zip(zipfolder, path): # function that appends disclaimer to both zipfiles in website data folder.
    print 'Appending to ' + zipfolder
    zf = zipfile.ZipFile(zipfolder, mode= 'a')
    try:
        zf.write(path, os.path.basename(path))
    finally:
        zf.close()
path = r'\\gisfile\GISstaff\Jared\Python Scripts\Data\Disclaimer - Final.txt'
zipfolders = [r'Z:\Data\WillCounty_AddressPoints.zip', r'Z:\Data\WillCounty_Streets.zip'] #list of zip folders
for zipfolder in zipfolders: # loop through zipfolders
    append2Zip(zipfolder, path) #call function above

Outcomes