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?
2 Answers 2
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– Julia Jiang2014年10月23日 23:52:34 +00:00Commented Oct 23, 2014 at 23:52
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– Julia Jiang2014年10月23日 05:01:02 +00:00Commented Oct 23, 2014 at 5:01
-
1@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.2014年10月23日 06:36:48 +00:00Commented Oct 23, 2014 at 6:36
Explore related questions
See similar questions with these tags.
for row in cursor
since that's the name you've given to the cursor a few lines earlier