overwrite output not overwriting?

07-05-2017 11:51 AM
Occasional Contributor

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'):
        print 'All files written to %s' %zip_path

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')
        zf.write(path, os.path.basename(path))
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
0 Kudos
6 Replies
MVP Regular Contributor

Your append2Zip() function is using the zipfile module, which isn't affected by arcpy.env. You probably need to use the os module to check if the file/directory exists, then delete it before creating the new zip file.

Occasional Contributor


I'm looking into it. Just to show you, here are the results after running the script in PythonWin with multiple copies of the txt files already in the folder. If I delete the txt files from the zipfiles and run it again the print statements are as I have them set up to be.

Occasional Contributor II

I agree with Blake.  If you did some type of check on the file/directory if it exists first:

if os.path.exists(zipfolder):
    zf.write(path, os.path.basename(path))
Occasional Contributor

Thanks for the help. I tried those few lines in a few places, but apparently not the right one as the txt files still aren't overwriting. 

so, I tried this in the place of the ZipShapes block and ran it as a script tool with no parameters:

if __name__ == '__main__': # calls Zipshapes module and runs it. This module zips shapefiles and saves in website data folder.
    path = r"\\gisfile\GISstaff\Jared\Python Scripts\Data"
    out_path = r"Z:\Data"
    ZipShapes(path, out_path)

But it tells me ZipShapes isn't defined (below). If I run the script on its own it runs perfectly.

0 Kudos
MVP Legendary Contributor

ZipShapes appears nowhere in your above code sample ie... the function must also be included before your 'if __name__=='__main__'  line

MVP Regular Contributor

Shouldn't the else be removed so it will always execute the zf.write() line? Also seems like it should be writing the zip file to the same place os.path.exists() is checking.

if os.path.exists(path):

zf.write(path, os.path.basename(path))‍‍‍‍‍‍‍‍
0 Kudos