Tips & Tricks: FieldExists for ArcGIS 10 Python

Here is a way to check if a Field in a feature class already exists or not. I was looking for an existing ArcGIS Esri function / tool but couldn’t find one so I wrote my own.

def FieldExist(featureclass, fieldname):
    fieldList = arcpy.ListFields(featureclass, fieldname)

    fieldCount = len(fieldList)

    if (fieldCount == 1):
        return True
    else:
        return False

This is a function and it assumes you have already set the workspace environment.

Usage:

        if (not FieldExist(myFeatureClass, "myField")):
          arcpy.AddError("Field 'myField' does not exist in " + myFeatureClass)
          sys.exit()

hope this is helpful.

3 Comments.

  1. Uhhhhhhhhhh! Good tool gangster

  2. Darryl Klassen

    another way to do it:
    if “myfield” in [f.name for f in arcpy.ListFields(myFeatureClass)]:
    arcpy.AddError(“……..

    • bjornkuiper

      thanks, that looks even easier. although the user should do an if-not. Anyway, it seems you have a good grasp of the python language. Thanks!