I am running this extremely simple code using the ArcGIS python environment:

import arcpy 
test = r"C:\....\New_Shapefile.shp"  #test contains several fields, including one called "Number".
updaterows = arcpy.SearchCursor(test) 
for row in updaterows:
    print (row.getValue("Number"))

the first time I run the code, it works and I get a list of all the elements in the column "Number", but if I try to run the for loop again, when I enter the code I do not receive any output. This does not change nor if I type the code again or if I simply recall it from the command history. Also, I tried to replace the field "Number" with some other field name, and it still does not print if already a print command was run before. It seems to work only if I delete "updaterows" first. What does this occur? Is the problem involved with the function or with the arcGIS development environment for python?

  • 1
    Please edit your Question to contain the version of ArcGIS in use. You'll also need to provide the code that doesn't work. There is no non-Retired version of ArcGIS that doesn't support DA cursors, so explaining why you've chosen to use a non-DA cursor (and to avoid best practice in the use of that cursor) should be included as well. – Vince Mar 2 '16 at 15:50

You need to delete the cursor objects and recreate them before running the code again

So after you run your code the first time, run this

del row, updaterows
updaterows = arcpy.SearchCursor(test) 
for row in updaterows:
    print (row.getValue("Number"))

See this question

| improve this answer | |
  • Oh OK then there is no other way. May I ask why python does not understand it otherwise? (if there is a simple answer to this question) – Patapunfate Mar 2 '16 at 15:58
  • @TommasoSacco, because the cursor is at the end of the table. You could use updaterows.reset() to send it back to the top of the table. – Tom Mar 2 '16 at 16:34
  • @TommasoSacco, I lied. Apparently reset() doesn't work on certain storage formats. That comes directly from ESRI, though it isn't mentioned in their documentation for the method. geonet.esri.com/thread/57314 – Tom Mar 2 '16 at 16:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.