1

This code works in the arcgis python window but in PyScripter, this warning message had been written:

Traceback (most recent call last): File "", line 16, in File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy_init_.py", line 1167, in SearchCursor return gp.searchCursor(dataset, where_clause, spatial_reference, fields, sort_fields) File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing_base.py", line 359, in searchCursor self._gp.SearchCursor(*gp_fixargs(args, True))) IOError:

import arcpy
arcpy.env.workspace = r"C:\Desktop\spatial statistics analysis tutorials\PatternAnalysis\PatternAnalysisExercise\DengueData.gdb"

# place all the rows from the feature class into a search cursor
cursor = arcpy.SearchCursor("IndCases")

# iterate through the cursor and print the attributes to the interactive window
# print it in a standard three line format
for row in cur:
    print row.AGE

I was wondering why this situation happened, any suggestions?

  • Hi Julia, welcome to GIS Stack Exchange. I've edited your code to make it more readable - you don't need to include all the comments, just the sections which are causing the problems. Can you please let us know the exact wording of the error message you're seeing? – Stephen Lead Oct 23 '14 at 2:42
  • 1
    PS it looks like your code should read for row in cursor since that's the name you've given to the cursor a few lines earlier – Stephen Lead Oct 23 '14 at 2:43
  • thank you, the error message is:Traceback (most recent call last): File "<module3>", line 16, in <module> File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy_init_.py", line 1167, in SearchCursor return gp.searchCursor(dataset, where_clause, spatial_reference, fields, sort_fields) File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing_base.py", line 359, in searchCursor self._gp.SearchCursor(*gp_fixargs(args, True))) IOError: <unprintable IOError object> – Julia Jiang Oct 23 '14 at 2:47
3

It could be an issue with the workspace path. if this is a win7 machine, i would not typically expect to see a path like

arcpy.env.workspace = r"C:\Desktop\spatial statistics analysis tutorials\PatternAnalysis\PatternAnalysisExercise\DengueData.gdb".

Instead, i would expect a path like

arcpy.env.workspace = r'C:\users\YourUserName\Desktop\...'

in a quick test, i found that arcpy will happily set the workspace to a non-existent path- only to later fail because it can't find the feature class.

  • yes, it works!!! thank you so much. At the beginning i copied the workspace path from ArcCatalog and this path was not include the "C:\users\YourUserName\"part. – Julia Jiang Oct 23 '14 at 23:52
3

1) You create a SearchCursor object which is assigned to the variable cursor:

# place all the rows from the feature class into a search cursor
cursor = arcpy.SearchCursor("IndCases")

But then you try to access this using the variable cur, which doesn't exist:

for row in cur:

Try using for row in cursor instead.

2) See the arcpy.da.SearchCursor help file for a better way to handle cursors in ArcGIS 10.2 and above:

with arcpy.da.SearchCursor(fc, fields) as cursor:
    for row in cursor:
        print(row)
  • Stephen, thank you. i edit the code as your instruction. but still has the same error message. and here is the new python code which i wrote: # import modules import arcpy,os # set the workspace gdb = r"C:\Desktop\spatial statistics analysis tutorials\PatternAnalysis\PatternAnalysisExercise\DengueData.gdb" # place all the rows from the feature class into a search cursor fc = os.path.join(gdb,r"IndCases") cur = arcpy.SearchCursor(fc) # iterate through the cursor and print the attributes to the interactive window # print it in a standard three line format for row in cur: print (row) – Julia Jiang Oct 23 '14 at 5:01
  • @JuliaJiang As you are probably discovering the Comments are not intended for code formatting or anything more than seeking quick clarifications to improve questions and answers. Since this answer has highlighted and addressed an error in your original question, you may want to consider asking a new and more detailed question that includes both the precise code snippet you are now using and the precise error message returned. – PolyGeo Oct 23 '14 at 6:36

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.