8

Can someone help me understand what the last 2 lines of this code does:

import arcpy
arcpy.env.workspace = "c:/esripress/python/data/exercise07"
arcpy.env.overwriteOutput = True
copy = arcpy.CopyFeatures_management("airports.shp","Results/airports.shp")
fc = "Results/airports.shp"
cursor = arcpy.da.UpdateCursor(fc, ["STATE"], ' "STATE" <> \'AK\'')
for row in cursor:
 row[0] = "AK"
 cursor.updateRow(row)
del row
del cursor

I understand that the loop function goes through each record that does not have a value of 'AK' and gives that record a value of "AK". But what I don't understand is what the del row and del cursor are meant to do.

Aaron
52k30 gold badges161 silver badges326 bronze badges
asked Sep 1, 2017 at 3:10
3
  • 2
    Incidentally this code probably corrupts the State value of any airport that was not previously in Alaska Commented Sep 1, 2017 at 4:11
  • 3
    Stephen has written a good answer however he hasn't illuminated why it's important to release (delete) row/cursor objects. An open cursor or row object leaves a lock on the feature class which will cause problems when trying to make any changes to the feature class until the session holding the locks is terminated, usually by closing the application but can be as severe as restarting your computer. As you only have one field it doesn't need to be a list (just 'state', not ['state'] but that wont stop it running; it will however overwrite any state that's not 'AK' with "AK" as Stephen said. Commented Sep 1, 2017 at 4:59
  • Thanks for your input , everything you guys said made perfect sense. *Note, the airports shapefile is of airports in AK, I was populating records that had no value for STATE. Commented Sep 1, 2017 at 5:39

1 Answer 1

13

Those are relics of an earlier style of arcpy cursors. del row, cursor were previously used to clean-up after the script was run by deleting the row and cursor objects. Now, the proper usage is to wrap the cursor in a with statement, which both opens and closes the row and cursor objects, as follows:

import arcpy
arcpy.env.workspace = "c:/esripress/python/data/exercise07"
arcpy.env.overwriteOutput = True
copy = arcpy.CopyFeatures_management("airports.shp","Results/airports.shp")
fc = "Results/airports.shp"
with arcpy.da.UpdateCursor(fc, ["STATE"], ' "STATE" <> \'AK\'') as cursor:
 for row in cursor:
 row[0] = "AK"
 cursor.updateRow(row)
answered Sep 1, 2017 at 3:43
4
  • 1
    Your answer is not entirely correct. You can still (and need to, in some cases) use the old method of creating the server object and deleting it when done. For example for insert operations, it is easier to do it that way sometimes (pro.arcgis.com/en/pro-app/arcpy/data-access/…). The main advantage of the with statement is that it automatically removes the lock on the data if the operation fails, which the other method does not do. Create cursor - error - it skips the del part and your data are locked. Commented Sep 1, 2017 at 13:19
  • 1
    @PhilippNagel when would you need to use the old method of creating cursors? Commented Sep 22, 2018 at 18:51
  • 1
    Now that I think about it, I'm not sure what I was thinking of back then. Commented Sep 22, 2018 at 21:25
  • From UpdateCursor page "Update cursors also support with statements to reset iteration and aid in removal of locks. However, using a del statement to delete the object or wrapping the cursor in a function to have the cursor object go out of scope should be considered to guard against all locking cases." My understanding of that, in other words: To make entirely sure there is something not hanging, you still better use "del row, cursor". Or you can wrap it into function so with end of function all in local scope gets automatically deleted. Commented Mar 28, 2019 at 0:18

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.