Quantcast
Channel: Question and Answer » arcpy
Viewing all articles
Browse latest Browse all 767

How to get around "name 'row' is not defined" error when row in question doesn't exist?

$
0
0

I am getting a “name ‘row’ is not defined” error when I run a particular piece of code (see below). This only happens when my input layer has one entry instead of two. So when the attribute query is run to find a second row there is no row to find and hence the error appears.

The aim of my overall script mean this will happen from time to time, what I am looking for is a way to accommodate this error and either avoid this section of script when an error is likely to occur or continue with the script even if this error pops up. I have tried to incorporate a ‘try’ and ‘except’ statement into the loop but have been unsuccessful. Would this work? if so how? If anyone has an idea on how to get around this problem please let me know.

import arcpy
from arcpy import env

env.workspace = "C:MyArcGISEDRN_Shp"

arcpy.env.overwriteOutput = True

join110 = "C:\MyArcGIS\EDRN_Shp\join110.shp"
joinField = "LCC_DRN_ID"
query1 ='"FID" = 1'   

arcpy.MakeFeatureLayer_management(join110, "feature_layer12")
arcpy.SelectLayerByAttribute_management ("feature_layer12", "NEW_SELECTION", query1)

rows = arcpy.SearchCursor("feature_layer12")
for row in rows:
    if row.getValue("edrn_in") >= "E":
        arcpy.JoinField_management("feature_layer10", joinField, "feature_layer12", joinField, "edrn_in")
    if row.getValue("edrn_out") >= "E":
        arcpy.JoinField_management("feature_layer10", joinField, "feature_layer12", joinField, "edrn_out")

del row, rows

Viewing all articles
Browse latest Browse all 767

Trending Articles