I did not realize that I should have been deleting cursors after accessing attribute tables until today. Up until now I never did this, but I've noticed that there are lots of file locks after my script runs (I presume it was a bad practice on my part to begin with). ArcGIS 10 help tells me that I should use the following code:

# Delete cursor and row objects to remove locks on the data 
del row 
del rows

I'm doing that in the following function:

# calculate areas
def area_calc( shapefile ):
    "calculates geometric properties of each building"      

    # loop through attribute table    
    Rows = ARCPY.UpdateCursor( shapefile ) 

    for row in Rows:

        geom = row.Shape

        polyArea = geom.area
        row.setValue( perimName, 0 )  # set to zero
        polyPerim = geom.length

        row.setValue( areaName, polyArea )   # update area value
        row.setValue( perimName, polyPerim ) # update perimeter value       


    # Delete the row and cursor    
    del row 
    del Rows

    ARCPY.AddMessage( 'Area and Perimeter calculated for each polygon' )

However, when I do that I get the following error message:

Traceback (most recent call last):
  File "LoopModuleBuildingAnalysis.py", line 9, in <module>
    import ConstructionMaterialPhase1
  File "D:\Code\AnalysisPhase1.py", line 101
    del row
SyntaxError: invalid syntax
Press any key to continue . . . 

Any suggestions what I should be doing?

  • Does 'del row' need to be indented one stop? Mar 3 '11 at 13:21
  • I don't think so - the help file does not do this, and it makes sense (to me) that it should be after the loop completes; though I don't know how row is seen later in the program, as it seems to be out of scope.
    – djq
    Mar 3 '11 at 14:12
  • Is this a file geodb? If so, did you try creating a new filegdb, then copying all the contents to the new filegdb. I wouldn't think locks would get copied. Mar 3 '11 at 15:01
  • It's not a file geodb; I think the problem may have been just the del row, though it's still crashing half-way through running and I see locks on earlier files, so I'm not sure....
    – djq
    Mar 3 '11 at 15:07
  • Is the code above the actual failing code? A copy/paste of the function above works fine for me. An indentation problem usually gives a IndentationError not a SyntaxError. Mar 3 '11 at 18:32

Deleting an unbound name will raise NameError, not SyntaxError. The following code, roughly equivalent, runs fine

>>> def foo():
...     for x in range(0, 5):
...         print x
...     del x
>>> foo()

Check to see that your for block is indented properly and that you don't have any wacky mix of tabs and spaces.

  • I think the problem was with the spacing in the help file that I copied. However, I'm a little confused - is it necessary to delete row? It just exists within the loop as far as I can tell - I suppose it could be deleted on every loop iteration, but that seems inefficient.
    – djq
    Mar 4 '11 at 13:05

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.