Ever wanted to know if a certain field exists in a feature class or attribute table? This could be to either populate it with something if it does exist or create it first if it does not exist, then populate it. The easy steps below will show you how to check if a field exists. If it does not exist, it will be created then perform a field calculation on it.
First is the code (function) to check if a field exists (Note that the green text is purely some metadata about this function):
[code language=”python”]
def fieldExists(dataset, field_name):
"""fieldExists(dataset, field_name)
Determines the existence ofa field in the specified data object. Tests
for the existence of a field in feature classes, tables, datasets,
shapefiles, workspaces, layers, and files in the current workspace. The
function returns a Boolean indicating if the element exists.
dataset(String):
The name, path, or both of a feature class, table, dataset, layer,
shapefile, workspace, or file to be checked for existence of the
specified field.
field_name(String):
The name of the field to be checked for existence"""
if field_name in [field.name for field in arcpy.ListFields(dataset)]:
return True
[/code]
Next we will work with this code (known as calling this function) to check if a specific field name exists in our feature class. The path to our feature class is C:\data\MyData.gdb\TestFeatureClass and the field name we are going to check for is CATEGORY. These we will set in a variable as such:
[code language=”python”]
featureclass = r"C:\data\MyData.gdb\TestFeatureClass"
fieldName = "CATEGORY"
[/code]
Because the fieldExists function with return a Boolean of True, we can use an if statement to do something if it does
exist. We do that by using the following:
[code language=”python”]
if fieldExists(featureclass, fieldName):
[/code]
Now we need to do something if it does exist. For now we will just return a message to say that it does exit (if it actually does exist in the feature class):
[code language=”python”]
print ("Yes, {0} field exists in {1}".format(fieldName, featureclass))
[/code]
If this field does exist in the feature class, the message returned will look like this:
image005
At the moment, if the field does not exist, no message will be shown. This also means that if it does not exist, you cannot do anything else. What we now need to do is write something to say that if the field does not exist in my feature class, I must do something else. This is done by using the else statement under the if statement (that’s logical, don’t you think). This is done like so:
[code language=”python”]
else:
[/code]
Pretty simple so far? Great!
Now we need to add a message to say that the field does not exist in the feature class:
[code language=”python”]
print ("No, {0} field does not exist in {1}".format(fieldName, featureclass))
[/code]
If this field does not exist in the feature class, the message returned will look like this:
image008
After that and using the same indentation as the print statement you can now use something like arcpy.AddField_management() to add the missing field which needs to be populated.
The completed script looks like this (You can copy and paste the code below and re-use it in your own script):
[code language=”python”]
import arcpy # don’t forget to import arcpy
def fieldExists(dataset, field_name):
if field_name in [field.name for field in arcpy.ListFields(dataset)]:
return True
featureclass = r"C:\data\MyData.gdb\TestFeatureClass"
fieldName = "CATEGORY"
if fieldExists(featureclass, fieldName):
print ("Yes, {0} field exists in {1}".format(fieldName, featureclass))
else:
print("No, {0} field does not exist in {1}".format(fieldName, featureclass))
# arcpy.AddField_management()
[/code]