Check to see if a field exists using Python

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):

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

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:

featureclass = r"C:\data\MyData.gdb\TestFeatureClass"
fieldName = "CATEGORY"

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:

if fieldExists(featureclass, fieldName):

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):

    print ("Yes, {0} field exists in {1}".format(fieldName, featureclass))

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:

else:

Pretty simple so far? Great!

Now we need to add a message to say that the field does not exist in the feature class:

   print ("No, {0} field does not exist in {1}".format(fieldName, featureclass))

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):

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()
  

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s