# Name: Append.py
# Description: Use the Append tool to combine several shapefiles
# Import system modules
import arcpy
import os
# Set environment settings
arcpy.env.workspace = "C:/data"
# Set local variables
outLocation = "C:/Output"
outName = "MA_towns.shp"
schemaType = "NO_TEST"
fieldMappings = ""
subtype = ""
# Process: Create a new empty feature class to append shapefiles to
emptyFC = arcpy.CreateFeatureclass_management(outLocation, outName, "POLYGON",
"amherst.shp")
# All polygon FCs in the workspace are MA town shapefiles, you want to append
# these to the empty FC. The list will resemble ["amherst.shp", "hadley.shp",
# "pelham.shp", "coldspring.shp"]
fcList = arcpy.ListFeatureClasses("", "POLYGON")
# Create FieldMappings object to manage merge output fields
fieldMappings = arcpy.FieldMappings()
# Add the target table to the field mappings class to set the schema
fieldMappings.addTable(emptyFC)
# Add input fields for the town name to TOWNNAME field that matches the
# target dataset since each input dataset has a different field name for
# this info
fldMap = arcpy.FieldMap()
fldMap.addInputField("amherst.shp","TOWNNAME")
fldMap.addInputField("hadley.shp","NAME")
fldMap.addInputField("pelham.shp","TOWN_NAME")
fldMap.addInputField("coldspring.shp","TOWN")
# Set name of new output field "TOWNNAME"
townName = fldMap.outputField
townName.name, townName.aliasName, townName.type = "TOWNNAME", "TOWNNAME", "TEXT"
fldMap.outputField = townName
# Add output field to field mappings object
fieldMappings.addFieldMap(fldMap)
# Do the same thing for the POPULATION field
fldMap = arcpy.FieldMap()
fldMap.addInputField("amherst.shp","POPULATION")
fldMap.addInputField("hadley.shp","POP")
fldMap.addInputField("pelham.shp","POP_2010")
fldMap.addInputField("coldspring.shp","POP")
# Set name of new output field "POPULATION"
pop = fldMap.outputField
pop.name, pop.aliasName, pop.type = "POPULATION", "POPULATION", "LONG"
fldMap.outputField = pop
# Add output field to field mappings object
fieldMappings.addFieldMap(fldMap)
# Process: Append the feature classes to the empty feature class
arcpy.Append_management(fcList, os.path.join(outLocation, emptyFC), schemaType,
fieldMappings, subtype)