6

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.

  • 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...? – John Jan 16 '15 at 19:48
  • @John I would like to add it to every feature class in a database. – EmilyF Jan 16 '15 at 19:49
16

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

| improve this answer | |
  • 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. – Fezter Nov 30 '16 at 1:41
  • Thanks @Fezter for pointing that out, I've edited the answer and removed the os module. – Aaron Nov 30 '16 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? – FaCoffee Jun 20 '17 at 8:48
  • @FaCoffee myfc1 is the name of the feature class. It is accessed by calling the iterable fc within the for loop. – Aaron Jun 21 '17 at 21:44
  • So I suppose I can change row[0]=fc to whatever I want it to be – FaCoffee Jun 21 '17 at 22:02
6

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)
| improve this answer | |
  • 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#//… – John Jan 16 '15 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? – EmilyF Jan 16 '15 at 19:57
  • 1
    @EmilyF this line in phloem's code will do that: arcpy.CalculateField_management(fc,"VALUE",1) – Dan C Jan 16 '15 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. – John Jan 16 '15 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:
     ...
| improve this answer | |
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?

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.