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.
Answer
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)
No comments:
Post a Comment