Tag Archives: python

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.

  1. def FieldExist(featureclass, fieldname):  
  2.     fieldList = arcpy.ListFields(featureclass, fieldname)  
  3.   
  4.     fieldCount = len(fieldList)  
  5.   
  6.     if (fieldCount == 1):  
  7.         return True  
  8.     else:  
  9.         return False  

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

Usage:

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

hope this is helpful.