1

I am trying to automate the creation of many different feature layers were their content is created by a cursor. Sometimes during the automation, a feature layer may be empty, this is okay, but as the cursor runs through the layer adding in the data, the del row line returns the error:

"NameError: name 'row' is not defined" and stops the script.

Question: If a layer I am creating happens to be empty, how can I skip over any errors that might pop up due to the cursor not being able to define itself in an empty table?

Already Tried: I tried setting a 'count' variable to how many rows are in each table, and telling python to skip over the table if it has less than 1 row, but the "del row" line still caused a fuss, and I had trouble setting up the try statement.

The code below is still returning the error. It is not printing empty cursor yet the ACount is indeed 0.

cursor = arcpy.da.UpdateCursor("A_Squares", ["LEGEND"])
ACount = arcpy.GetCount_management("A_Squares")
print ACount
if ACount == 0:
    print "empty cursor"
else:
    for row in cursor:
        row[0] = TableA
        cursor.updateRow(row)
    del row
    del cursor

Any ideas?

  • 2
    If you are using the GetCount tool to get the number of rows and using that to skip tables that sounds a very sensible way of doing it. So if that is your control over if a loop executes then it sounds like you are destroying rows when none were ever created? Without seeing the code I would suggest you review the logic of your code and look at when you create and destroy row objects. – Hornbydd Aug 26 '14 at 16:55
3

The output from the GetCount method is not fully correct, so your if statement is not going down the correct path. Use the .GetOutput method:

count = arcpy.GetCount_management("A_Squares")
ACount = int(count.getOutput(0))

if ACount == 0:
    print "empty cursor"
else:
    for row in cursor:
        #...etc
1

It may not be completely necessary to delete the row object, but if you just define it first you should be golden.

cursor = arcpy.SearchCursor("whatever")
row = None # Define it here

for row in iter(cursor.next, None):
    # ...

del cursor
del row

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.