Python Script just stopped working..totally stumped

4516
2
Jump to solution
04-18-2013 05:44 AM
JohnCarr2
New Contributor
Hey everyone, I have a simple script pasted below that searches through a field containing numbers (the field is type double) in a feature class and returns what would be the next highest unused number in the field. I ran this script probably 20 times yesterday and it worked fine, then all the sudden it stopped working and I started getting this error...

Runtime error
Traceback (most recent call last):
  File "<string>", line 10, in <module>
NameError: name 'AccessDBNo' is not defined

Any ideas? I've never had something work and then suddenly not work with no changes made to the script whatsoever.

import arcpy
from arcpy import env
env.workspace = r"Database Connections\PTWP_Base.sde\PTWP_BASE.DBO.Propose_Facilities"
fc = "PTWP_BASE.DBO.Eval_Seg"

numbers = []
rows = arcpy.SearchCursor(fc)
for row in rows:
    numbers.append(row.getValue(AccessDBNo))
del row
   
numbers.sort()

max = numbers[-1]

NextNo=max+1

arcpy.AddMessage("The next available project number is...")
arcpy.AddMessage(NextNo)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
I think you were getting that error because your field AccessDBNo was referenced like a variable rather than a string for the field.  This would not be a problem had you actually created the variable before passing it into the getValue().  If you use the row.getValue() you have to have the field name inside the parenthesis as a string or you can use a variable to represent the string name of the field.  So you would do it like this:

# hard coded row.getValue("AccessDBNo")


OR

# uses variable field = "AccessDBNo" rows = arcpy.SearchCursor(fc) for row in rows:     numbers.append(row.getValue(field)) del row, rows 


The getValue method is most useful when the field is stored as a variable so you do not have to hard code the field name in.  Alternatively you can use this method to get the value:

row.AccessDBNo


I think that was the problem in your code.  I have made a few changes to your original code to use the built in max() function.  I also renamed your "max" variable because this is a reserved word for the max() function.  As to why it worked 20 times then randomly stopped, I do not know why that would happen.  Hopefully this works:

import arcpy from arcpy import env env.workspace = r"Database Connections\PTWP_Base.sde\PTWP_BASE.DBO.Propose_Facilities" fc = "PTWP_BASE.DBO.Eval_Seg"  numbers = [] rows = arcpy.SearchCursor(fc) for row in rows:     numbers.append(row.AccessDBNo) del row, rows  # Get maximum using max() function, max is a reserved word for max() function.   maxi = max(numbers)  NextNo = maxi + 1  arcpy.AddMessage("The next available project number is...") arcpy.AddMessage(NextNo) 

View solution in original post

0 Kudos
2 Replies
by Anonymous User
Not applicable
I think you were getting that error because your field AccessDBNo was referenced like a variable rather than a string for the field.  This would not be a problem had you actually created the variable before passing it into the getValue().  If you use the row.getValue() you have to have the field name inside the parenthesis as a string or you can use a variable to represent the string name of the field.  So you would do it like this:

# hard coded row.getValue("AccessDBNo")


OR

# uses variable field = "AccessDBNo" rows = arcpy.SearchCursor(fc) for row in rows:     numbers.append(row.getValue(field)) del row, rows 


The getValue method is most useful when the field is stored as a variable so you do not have to hard code the field name in.  Alternatively you can use this method to get the value:

row.AccessDBNo


I think that was the problem in your code.  I have made a few changes to your original code to use the built in max() function.  I also renamed your "max" variable because this is a reserved word for the max() function.  As to why it worked 20 times then randomly stopped, I do not know why that would happen.  Hopefully this works:

import arcpy from arcpy import env env.workspace = r"Database Connections\PTWP_Base.sde\PTWP_BASE.DBO.Propose_Facilities" fc = "PTWP_BASE.DBO.Eval_Seg"  numbers = [] rows = arcpy.SearchCursor(fc) for row in rows:     numbers.append(row.AccessDBNo) del row, rows  # Get maximum using max() function, max is a reserved word for max() function.   maxi = max(numbers)  NextNo = maxi + 1  arcpy.AddMessage("The next available project number is...") arcpy.AddMessage(NextNo) 
0 Kudos
JohnCarr2
New Contributor
Caleb, thanks that is exactly what the problem was. I should have caught that earlier but I think I was looking to deep and overlooked the obvious. Thanks again for the explanation!
0 Kudos