8

I have to try to add a field called 'Name' to a bunch of different feature classes using python, and then access the name of the feature class to fill in the field. I have no idea how to even start this! I know I need a loop but that's all I know.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jan 16, 2015 at 19:33
2
  • 1
    You'd need to provide a bit more information for anyone to give you the most applicable answer, especially, where are these feature classes? Do you have a list of random feature classes from all over, are you trying to add this field to every feature class in a specific database, are you needing to add it to every feature class called "XYZ_FC" in every database on your computer, etc...? Commented Jan 16, 2015 at 19:48
  • @John I would like to add it to every feature class in a database. Commented Jan 16, 2015 at 19:49

4 Answers 4

17

Here is a cursor-based approach, which I usually default to as syntax is simpler than using Calculate Field. This is the workflow:

import your module

import arcpy

Set the workspace so that Python knows where to look for the feature classes. In this case, it is a GDB

arcpy.env.workspace = r'C:\temp2\my_gdb.gdb'

Start a loop and iterate over the feature classes in the GDB

for fc in arcpy.ListFeatureClasses():

Add a text field called "Name" of length 50

arcpy.AddField_management(fc, "Name", "TEXT", field_length = 50)

Within each feature class attribute table, write the name of the current FC

with arcpy.da.UpdateCursor(fc, "Name") as cursor:
 for row in cursor:
 row[0] = fc
 cursor.updateRow(row)

import arcpy
arcpy.env.workspace = r'C:\temp2\my_gdb.gdb'
for fc in arcpy.ListFeatureClasses():
 arcpy.AddField_management(fc, "Name", "TEXT", field_length = 50)
 with arcpy.da.UpdateCursor(fc, "Name") as cursor:
 for row in cursor:
 row[0] = fc
 cursor.updateRow(row)

enter image description here

answered Jan 16, 2015 at 20:03
6
  • While I typically include the os module for good measure, it's worth pointing out that it isn't necessary to import it for this code to work. Commented Nov 30, 2016 at 1:41
  • Thanks @Fezter for pointing that out, I've edited the answer and removed the os module. Commented Nov 30, 2016 at 3:23
  • Very clean example! But... the myfc1 I read in the screenshot... where does it come from? Is that the value of fc? Commented Jun 20, 2017 at 8:48
  • @FaCoffee myfc1 is the name of the feature class. It is accessed by calling the iterable fc within the for loop. Commented Jun 21, 2017 at 21:44
  • So I suppose I can change row[0]=fc to whatever I want it to be Commented Jun 21, 2017 at 22:02
7

I happen to be doing something similar this morning. This script makes use of the Current Workspace environment variable, listing data, adding a field, and calculating it:

# set workspace environment to your GDB
arcpy.env.workspace = r"C:\junk\db.gdb"
# list the feature classes
fcList = arcpy.ListFeatureClasses()
# loop through list
for fc in fcList:
 #check if field exists, if not, add it and calculate
 if "VALUE" not in arcpy.ListFields(fc):
 arcpy.AddField_management(fc,"VALUE","TEXT")
 arcpy.CalculateField_management(fc,"VALUE",fc)
answered Jan 16, 2015 at 19:51
4
  • 2
    While I agree with this answer, I thought it might be helpful to include the link to the AddField documentation in case you need help figuring out how to set the parameters correctly for adding the field (ex: needing to change the field type which this example has set to "SHORT" which would be numeric). That documentation can be found at resources.arcgis.com/en/help/main/10.1/index.html#//… Commented Jan 16, 2015 at 19:55
  • @phloem would I do something similar to this loop to fill in the field next to it with the name of the feature class? Commented Jan 16, 2015 at 19:57
  • 1
    @EmilyF this line in phloem's code will do that: arcpy.CalculateField_management(fc,"VALUE",1) Commented Jan 16, 2015 at 19:59
  • 1
    @EmilyF, if you need to populate the newly created field with a value and a separate field with the name of the feature class, you could leave/modify the existing CalculateField_management entry to calculate the new field and then add another line at the bottom, indented the same amount, that's something like arcpy.CalculateField_management(fc,"Field2",fc) assuming Field2 is a text field of sufficient length. Commented Jan 16, 2015 at 20:03
4

Given the multiple answers using ListFeatureClasses() just wanted to add that if you have feature classes in feature datasets in your geodatabase you will need to add one extra loop to handle those feature classes.

fds = arcpy.ListDatasets()
for fd in fds:
 fcs = arcpy.ListFeatureClasses(feature_dataset=fd)
 for fc in fcs:
 ...
fcs = arcpy.ListFeatureClasses()
for fc in fcs:
 ...
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Jan 18, 2015 at 21:18
3

You were correct about the loop! You can use a loop to work through each feature class in the current workspace.

featureClasses = arcpy.ListFeatureClasses():
for fc in featureClasses:
 arcpy.AddField_management(fc,"Name","TEXT")

That would create a text field called "Name" in each feature class in your current workspace.

I would need a bit more information to help with the filling in part.

Does each feature class have features in it? how many? Does each feature get a different name?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Jan 16, 2015 at 20:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.