What I need to do:
- rename a field name of a table/feature class
- copy all values to the new field
So far I have done following code as artwork21 suggests:
import sys import traceback import arcpy from arcpy import env ## ARGUMENTS # argv = input table/feature class path # argv = input old field name # argv = input new field name path = sys.argv oldFieldName = sys.argv newFieldName = sys.argv env.overwriteOutput = True fields = arcpy.ListFields(path) for field in fields: if field.aliasName == oldFieldName: if not oldFieldName == newFieldName: fieldType = field.type # Add new field arcpy.AddField_management(path, newFieldName, fieldType) #Calculates the new field based on old field values arcpy.CalculateField_management(path, newFieldName, "!"+oldFieldName+"!", "PYTHON", "") # Delete the old field (if necessary) arcpy.DeleteField_management(path, oldFieldName)
How can I map
AddField_management method's field type? And while the field is in a middle place, then field deleted from middle and added to the last. That does not looks like as if field name is renamed. Is there any better solution that help me to do these things?