Database Reference
In-Depth Information
Adding and deleting fields
Python has created a geodatabase and a feature class; however, this feature class has only
the default attributes, the
OBJECTID
and
SHAPE
fields. It is time to learn how to let
Python add fields to an existing feature class. This is done through the
ar-
cpy.CreateFileGDB_management
command. The function takes a minimum of
three parameters: the full path of the feature class, the name of the field to be added, and
the type of the field. The full path to the feature class is basically the full path to the file
geodatabase followed by a slash (\) and then the feature class name.
To add fields to a feature class, perform the following steps:
1. Close all other sessions, open a new Python editor session, and write the following
code:
import arcpy
sfc_fullpath = "c:/gdb/my_Python_gdb.gdb/
my_Python_featureclass"
sfield_name = "my_Python_field"
sfield_type = "LONG"
arcpy.AddField_management(sfc_fullpath, sfield_name,
sfield_type)
input ("Field added successfully, press any key to
continue..")
2. Save the file as
add_field.py
under the
scripts
folder and then run it.
3. To verify that the field is added, open
ArcCatalog
and browse to the geodatabase.
Right-click on the feature class and click on
Properties
. As you can see in the fol-
lowing screenshot, the field was successfully created: