0

I have multiple feature classes in a geodatabase. I want to run a script that checks if a specific feature class exist within the geodatabase and if it does, modify specific fields found in that feature class.I want it to go through and find each feature class listed in the script and modify the fields. I have a beginning script that I exported from ModelBuilder to get a starting point, but I'm not sure how to put this into a loop.

# Import arcpy module
import arcpy


# Local variables:
FLANGE = "FLANGE"
FLANGE_Select = "\\\\\\My Documents\\ArcGIS\\Default.gdb\\FLANGE_Select"
FLANGE_Select__2_ = FLANGE_Select
FLANGE_Select__3_ = FLANGE_Select__2_
CASING = "CASING"
CASING_Select1 = "\\\\\\My Documents\\ArcGIS\\Default.gdb\\CASING_Select1"
CASING_Select1__2_ = CASING_Select1
CASING_Select1__3_ = CASING_Select1__2_
CASING_Select1__4_ = CASING_Select1__3_
CASING_Select1__5_ = CASING_Select1__4_
CASING_Select1__6_ = CASING_Select1__5_

# Process: Select
arcpy.Select_analysis(FLANGE, FLANGE_Select, "")

# Process: Alter Field (4)
arcpy.AlterField_management(FLANGE_Select, "WORK_ORDER", "Order", "New Order", "", "", "NON_NULLABLE", "false")

# Process: Alter Field (5)
arcpy.AlterField_management(FLANGE_Select__2_, "DATE_INSTALLED",    "Installed", "New Installed", "", "", "NON_NULLABLE", "false")

# Process: Select (2)
arcpy.Select_analysis(CASING, CASING_Select1, "")

# Process: Alter Field
arcpy.AlterField_management(CASING_Select1, "BEGIN_SERIES", "Series", "New Series", "", "", "NON_NULLABLE", "false")

# Process: Alter Field (2)
arcpy.AlterField_management(CASING_Select1__2_, "END_STATION", "Station", "New Station", "", "", "NON_NULLABLE", "false")

# Process: Alter Field (3)
arcpy.AlterField_management(CASING_Select1__3_, "EVENT_GUID", "Guid", "New Guid", "", "", "NON_NULLABLE", "false")

# Process: Delete Field
arcpy.DeleteField_management(CASING_Select1__4_, "USER")

# Process: Delete Field (2)
arcpy.DeleteField_management(CASING_Select1__5_, "ID")'
1

It's a little difficult to tell from the ModelBuilder export which fields you are attempting to modify.

Start by getting a list of the Feature Classes. This depends on having the workspace set to the geodatabase or folder where you expect to find the feature classes. You can use wildcards to only list specific feature classes, or can use if statement to check for it within the loop later.

See https://pro.arcgis.com/en/pro-app/tool-reference/data-management/alter-field-properties.htm for reference for Alter Field, which includes these setup steps.

#Import geoprocessing
import arcpy

#Set workspace
arcpy.env.workspace = r'c:\My Documents\ArcGIS\Default.gdb' # update this!

#Loop through feature classes looking for a fc named 'CASING_Select1':
fcList = arcpy.ListFeatureClasses() #get a list of feature classes
for fc in fcList:
    if fc == "CASING_Select1":
        field = "WORK_ORDER" 
        new_name = "Order"
        new_alias = "New Order"
        new_is_nullable = "NON_NULLABLE", "false"
        clear_alias="FALSE"
        arcpy.AlterField_management(fc, field, new_name, new_alias, new_type, new_is_nullable, clear_alias)

If you have a number of fields to change, you could set up a dictionary and loop through the items in the dictionary.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.