I'm writing a short script that iterates over a list of feature classes and sets values for a specific field ("Contour_Type"), based on the values in two other fields ("Type" and "Depression"). It seems my script does not pass the 'with arcpy.UpdateCursor(i) as cursor' line. I've searched for an answer, but can't find anything that pertains to my issue.

EDIT: The following code now works!

inputloc = arcpy.GetParameterAsText(0)
arcpy.env.workspace = inputloc
inputfcs = arcpy.ListFeatureClasses()

    for i in inputfcs:
        with arcpy.da.UpdateCursor(i,["Type","Depression", "Contour_Type"]) as cursor: 
            for typ, dep, con_typ in cursor:
                if typ == "Intermediate" and dep == "Yes":
                    arcpy.AddWarning("Adding Values..")
                    row = (typ, dep, "Intermediate_Depression")
                elif typ == "Index" and dep == "Yes":
                    row = (typ, dep, "Index_Depression")
                    row = (typ, dep, typ)
  • 3
    The "with" syntax is a Data Access cursor construct -- with arcpy.da.UpdateCursor – Vince Sep 1 '15 at 20:36
  • 1
    When presenting code snippets for help here I think it is usually best to do so without try/except statements because they can mask the errors that Python provides you. Those errors are very helpful for future visitors to find when they encounter the same problem. – PolyGeo Sep 1 '15 at 21:23
  • @PolyGeo This is useful advice, I'll keep it mind – Alec Patrizio Sep 2 '15 at 1:27

As Vince mentioned in the comments, 'The "with" syntax is a Data Access cursor construct'.

You have a couple options:

Using the old version of update cursor:

cursor = arcpy.UpdateCursor(i)
for row in cursor:
    if row.getValue("Type") == "Intermediate" and row.getValue("Depression") == "Yes":
    row.setValue("Contour_Type", "Intermediate_Depression")
del cursor

Using the (faster) Data Access cursor (requires 10.1+):

with arcpy.da.UpdateCursor(i, ["Type", "Depression", "Contour_Type"]) as cursor:
    for type, dep, con_type in cursor:
        if type == "Intermediate" and dep == "Yes":
            row = (type, dep, "Intermediate_Depression")
        elif type == "Index" and dep == "Yes":
            row = (type, dep, "Index_Depression")
            row = (type, dep, type)
  • 3
    Yes, this is absolutely correct. That is because the old cursors do not implement the __enter__ and __exit__ methods to allow with statements. – crmackey Sep 1 '15 at 21:57
  • 2
    Don't forget cursor = arcpy.UpdateCursor(i) really should be matched with a del cursor otherwise python will persistently lock the feature class which would cause problems renaming, moving or editing. When using with arcpy.da... the locks are released at the end of the block. – Michael Stimson Sep 1 '15 at 22:34
  • Good point Michael. I always add the del cursor after the da version as well. Does it matter? – Emil Brundage Sep 1 '15 at 22:50
  • 3
    It won't hurt anything to delete the da cursor object, but I believe Esri has logic built in to release the schema lock when the __exit__ method is called when the cursor has exhausted itself from the with statement. In other words, when the with statement is finished, there shouldn't be any locks on the data from the cursor. – crmackey Sep 1 '15 at 23:12
  • @EmilBrundage After some more research, I realized I was mixing the two different cursor constructs. Your answer clarifies this even more. I will try again at work tomorrow morning and update. Thanks! – Alec Patrizio Sep 2 '15 at 1:33

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.