1

I am trying to add 2 (multiple) fields to each feature class in a gdb. I based the script off of Python Script to Add Fields to Feature Classes.

I feel like I am missing something and can't figure it out.

import arcpy, os

arcpy.env.workspace = r'Z:\project\....gdb'

for fc in arcpy.ListFeatureClasses():
    try:
        arcpy.AddField_management(fc, "Depth", "SHORT", field_length = 10)
        arcpy.AddField_management(fc, "OD", "SHORT", field_length = 10)
        with arcpy.da.UpdateCursor(fc, "Depth", "OD") as cursor:
            for row in cursor:
                row[0] = fc
                cursor.updateRow(row)
                print "Working on {0}".format(fc)

    except Exception as ex:
        print "There was an error --->    {0}".format(ex)

print ("Add Field Finished")
7
  • 2
    You are adding integer fields and then trying to populate them with a string (the name of the feature class). What are you actually trying to do?
    – mr.adam
    Commented May 5, 2015 at 14:48
  • I guess that doesn't work. I am trying to add 2 fields to each feature class in a gdb (200+ total).
    – Mat S
    Commented May 5, 2015 at 14:51
  • 1
    What values would you like to populate the fields with? What error message are you getting
    – Aaron
    Commented May 5, 2015 at 14:59
  • 2
    I guess my question was, what is the UpdateCursor for? however, in looking at how you implemented it, it looks like you may be under the impression that it's necessary to add the fields. Don't worry, you don't need it! You only need the AddField operation. UpdateCursors are used to iterate all of the rows in feature class (or table) and change the attribute values. If you just want two new, empty fields, just remove the cursor.
    – mr.adam
    Commented May 5, 2015 at 15:15
  • 1
    It looks like everything after the last "AddField" is unnecessary. Like Aaron said, what are you trying to populate the fields with? It appears that you are trying to put the name of each feature class into a short integer field. Also, is there a reason that you are limiting the field length to 10?
    – GeoJohn
    Commented May 5, 2015 at 15:16

2 Answers 2

2

You need to put the fields you want to include in the cursor within a list or tuple. How you have it now, you are putting a WHERE clause of "OD".

arcpy.da.UpdateCursor(in_table, field_names, {where_clause}, {spatial_reference}, {explode_to_points}, {sql_clause})

You want:

with arcpy.da.UpdateCursor(fc, ("Depth", "OD")) as cursor:

With that said, I don't understand what you are trying to do with row[0] = fc

You're assigning the "DEPTH" field which is of integer data type a string value.

If you just want to add fields to each feature class, this part of your code should work:

import arcpy

arcpy.env.workspace = "Z:\\project\\....gdb"

for fc in arcpy.ListFeatureClasses():
    try:
        arcpy.AddField_management(fc, "Depth", "SHORT", field_length = 10)
        arcpy.AddField_management(fc, "OD", "SHORT", field_length = 10)

    except Exception as ex:
        print "There was an error --->    {0}".format(ex)

print("Add Field Finished")
1

I do this often without issue. The only difference between what I do, and what you're doing is I don't use the .da cursor. I use arcpy.UpdateCursor instead. So mine would look like:

import arcpy, os

arcpy.env.workspace = r'Z:\project\....gdb'

for fc in arcpy.ListFeatureClasses():
    try:
        arcpy.AddField_management(fc, "Depth", "SHORT", field_length = 10)
        arcpy.AddField_management(fc, "OD", "SHORT", field_length = 10)
        with arcpy.UpdateCursor(fc) as cursor:
            print "Working on {0}".format(fc)
            for row in cursor:
                row.FIELDNAMEHERE = fc #I am guessing you're assigning the name of the layer here?
                cursor.updateRow(row)


    except Exception as ex:
        print "There was an error --->    {0}".format(ex)

print ("Add Field Finished")

My usual way of implementing the code is:

layers = [#my list of layers here]
for layer in layers:
  arcpy.AddField_management(layer, "NEWFIELD", "TEXT")
  rows = arcpy.UpdateCursor(layer)
    for row in rows:
      row.NEWFIELD = "my new value"
      rows.updateRow(row)
  del rows
2
  • thanks for your sharing. would you plz explain the with...as block code, i could not understand it. thanks in advance. my goal is adding the multi fields and calculating the new fields. Commented Dec 1, 2015 at 23:53
  • with is the equivalent to a using block in languages like C#. It is a way to initiate and dispose of unmanaged resources in a single block. Check out stackoverflow.com/questions/1369526/… to get more information.
    – Branco
    Commented Dec 3, 2015 at 0:36

Your Answer

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

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