AnsweredAssumed Answered

if, else in searchcursor not working

Question asked by behrman on Aug 16, 2012
Latest reply on Aug 17, 2012 by behrman
The following code always returns false, even when true, and the 'AddError' after the 'else' doesn't trigger.  When I comment out the 'else:' it works fine, when it's true.  The 'except:' doesn't trigger when it's false either?  Any thoughts? 

import arcpy, os, sys stateName = arcpy.GetParameterAsText(0) gisNum = arcpy.GetParameterAsText(1) gisNumStr = "g" + gisNum + ".tif" arcpy.AddMessage("Searching.....") query = "Name = " + "'" + "g" + gisNum + ".tif" + "'" gisName = ("'" + "g" + gisNum + ".tif" + "'") mxd = arcpy.mapping.MapDocument("CURRENT") pdf = arcpy.mapping.ListDataFrames(mxd)[0] vLayer = arcpy.mapping.Layer(stateName) arcpy.mapping.ListLayers(mxd, stateName) try:     arcpy.AddMessage("Starting.....")     arcpy.AddMessage(gisNumStr)     rows = arcpy.SearchCursor(stateName)     for row in rows:          if row.getValue("Name") == gisNumStr:                      arcpy.AddMessage(gisNumStr)             arcpy.AddMessage(vLayer.name)             vLayer.definitionQuery = query             arcpy.AddMessage(query)             arcpy.SelectLayerByAttribute_management(vLayer, "NEW_SELECTION", query)             pdf.zoomToSelectedFeatures()             arcpy.RefreshActiveView ()              arcpy.SelectLayerByAttribute_management(vLayer, "CLEAR_SELECTION")             arcpy.RefreshActiveView ()             arcpy.AddMessage("Search completed..........")             del mxd, pdf, vLayer, query, row, rows         else:             arcpy.AddError("Item does not exist"); sys.exit()         del row         del rows except:     arcpy.AddError("No");sys.exit()      del row     del rows

Outcomes