AnsweredAssumed Answered

Python: calling three functions in one script?

Question asked by jared_pilbeam on Dec 1, 2016
Latest reply on Dec 4, 2016 by lpinner

I have three consecutive scripts that 1) update features in a map, 2) zip them, then copy them to a new folder, 3) append a .txt file to the zip. But, I can't quite grasp how to run all of them in order. I ran RemoveAddLayer.py assuming I was calling the other two scripts correctly. There was an error(s) (see very bottom).

 

###RemoveAddLayer.py###

import arcpy
import os
from arcpy import env

#import functions
import ZipFiles
import Append2Zip

arcpy.overwriteOutput = True

env.workspace = r"Database Connections\ims to plainfield.sde\gisedit.DBO.MGU_Will"

#set path to mxd
mxd = arcpy.mapping.MapDocument(r"Z:\Jared\Data_Request.mxd")

#reference the dataframe
for df in arcpy.mapping.ListDataFrames(mxd):
    print df
    #reference the layers to be removed
    for lyr in arcpy.mapping.ListLayers(mxd, "gisedit.DBO.Address_Points", df):
        arcpy.mapping.RemoveLayer(df, lyr)
        print lyr
        for lyr in arcpy.mapping.ListLayers(mxd, "gisedit.DBO.Street", df):
            arcpy.mapping.RemoveLayer(df, lyr)
            print lyr
            #reference the layers to be added
            addLayer = arcpy.mapping.Layer(r"Database Connections\ims to plainfield.sde\gisedit.DBO.MGU_Will\gisedit.DBO.Address_Points")
            arcpy.mapping.AddLayer(df, addLayer, "TOP")
            print addLayer
            addLayer = arcpy.mapping.Layer(r"Database Connections\ims to plainfield.sde\gisedit.DBO.MGU_Will\gisedit.DBO.Street")
            arcpy.mapping.AddLayer(df, addLayer, "TOP")
            print addLayer
           
            #delete old files
            if arcpy.Exists("WillCounty_AddressPoint"):
                arcpy.Delete_management("WillCounty_AddressPoint")

                if arcpy.Exists("WillCounty_Street"):
                    arcpy.Delete_management("WillCounty_Street")

                    #address points variables to be copied/saved in folder
                    inFeatures = "gisedit.DBO.Address_Points"
                    outLocation = r"Z:\Jared\Python Scripts\Data"
                    outFeatureClass = "WillCounty_AddressPoint"
                   
                    #execute and copy/save in folder. FeatureClassToFeatureClass_conversion syntax (in_features, out_path, out_name,
                    #{where_clause}, {field_mapping}, {config_keyword})
                    arcpy.FeatureClassToFeatureClass_conversion(inFeatures, outLocation, outFeatureClass)
                    print "finished Address_Point.shp"

                    #variables to be copied/saved in folder
                    inFeatures = "gisedit.DBO.Street"
                    outLocation = r"Z:\Jared\Python Scripts\Data"
                    outFeatureClass = "WillCounty_Street"

                    #execute and copy/save in folder. FeatureClassToFeatureClass_conversion syntax (in_features, out_path, out_name,
                    #{where_clause}, {field_mapping}, {config_keyword})
                    arcpy.FeatureClassToFeatureClass_conversion(inFeatures, outLocation, outFeatureClass)
                    print "finished Street.shp"
del mxd

if __name__ == '__main__':

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

    ZipShapes(path, out_path)

 

 

###ZipFiles.py

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

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]
        print name
        print shapes
        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 file written to %s' %zip_path
        zip.close()

 

 

###Append2Zip.py

def append2Zip(zipfolder, path): # define function; name of function followed by any arguments in ()
    print 'Appending to ' + zipfolder
    zf = zipfile.ZipFile(zipfolder, mode= 'a')
    try:
        zf.write(path, os.path.basename(path))
    finally:
        zf.close()

path = r'Z:\Jared\Disclaimer - Final.txt'
zipfolders = [r'W:\Data\WillCounty_AddressPoint.zip', r'W:\Data\WillCounty_Street.zip'] #list of zip folders

for zipfolder in zipfolders: # loop through zipfolders
    append2Zip(zipfolder, path) #call function above

 

 

 

Outcomes