Skip to content

Instantly share code, notes, and snippets.

@ustroetz
Created November 8, 2013 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ustroetz/7376623 to your computer and use it in GitHub Desktop.
Save ustroetz/7376623 to your computer and use it in GitHub Desktop.
# Name: Ulrich Stroetz (U102778)
# Date: 2/17/2013
# Purpose: Add fields
# Import system modules
import arcpy
# Input Feature Class)
inputFC = arcpy.GetParameterAsText(0)
# Create list of field names
inputString = arcpy.GetParameterAsText(1)
fieldListNew = inputString.split(";")
fieldType = arcpy.GetParameterAsText(2)
fieldListExist = [f.name for f in arcpy.ListFields(inputFC)]
# Add fields
for fieldName in fieldListNew:
# Check if it already exists
if fieldName in fieldListExist:
arcpy.AddMessage("The field %s already exists and will not be created." % fieldName)
# If not, add it
else:
fieldName_new = arcpy.ValidateFieldName(fieldName)
if fieldName_new != fieldName:
arcpy.AddMessage(fieldName + " becomes " + fieldName_new)
arcpy.AddField_management(inputFC , fieldName_new, fieldType)
arcpy.AddMessage("Created field:" + fieldName_new)
arcpy.AddMessage ("Process completed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment