Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a seemingly simple code which iterates through a list of feature classes and calculates an ID field in the attribute table that is equal to the first 5 characters of feature class name. The script works fine if I hard code the workspace. As soon as I change it to "Get Parameter as Text" I receive this error:

ERROR 000539: Error running expression: expression : name 'expression' is not defined Failed to execute (CalculateField).

The script is:

import arcpy, sys, array, time, datetime

#Get feature dataset
inws = arcpy.GetParameterAsText(0)

#Set geoprocessing environments
arcpy.env.workspace = inws
arcpy.env.overwriteOutput = True

#Iterate through the list of feature classes

fcList = arcpy.ListFeatureClasses()

for fc in fcList:
    print str("processing" + fc)

#Define field and expression
field = "SID"
expression = str(fc[:5])

#Create a new field name and expression
arcpy.AddField_management(fc, field, "TEXT")
arcpy.CalculateField_management(fc, field, "expression", "PYTHON")

does anyone know what is going on here? Thanks!

share|improve this question
add comment

3 Answers

I see two problems. First, the way it's indented won't iterate through the featureclasses and perform the process on each, it will just iterate through and print your print statement. Second, your expression should be built with quotes around it (at least that's the case for VB syntax) and called as a variable and not a string.

Try the following:

import arcpy, sys, array, time, datetime

#Get feature dataset
inws = arcpy.GetParameterAsText(0)

#Set geoprocessing environments
arcpy.env.workspace = inws
arcpy.env.overwriteOutput = True

#set the field name
field = "SID"

#Iterate through the list of feature classes   
fcList = arcpy.ListFeatureClasses()  
for fc in fcList:
    print str("processing" + fc)

    #Define expression
    expression = "\"" + fc[:5] + "\""

    #Create a new field name and expression
    arcpy.AddField_management(fc, field, "TEXT")
    arcpy.CalculateField_management(fc, field, expression)

Note that I took out the "PYTHON" option on CalculateField... I generally use the default VB syntax for simple operations, so I think this should work.

Also, touching on what R.K. was saying about GetParameterAsText(0) not being assigned a value, I'm assuming you're running this as a script tool in ArcMap in which you've mapped out an input to assign to Parameter(0). It should work in this case if your input is mapped as a workspace, otherwise if you're not running it as a script tool, (s)he's right, it needs input.

share|improve this answer
    
Thank you, this worked perfectly. I still don't understand though why my script only seemed to fail when I changed the input workspace (inws) to a Get parameter as Text) value. The script worked perfectly when I had the workspace hardcoded to a test database. Then, as soon as I changed it so it was suitable to add as a tool into toolbox, it failed with the expression. Thanks again though- I really appreciate your feedback! –  user10723 Oct 5 '12 at 17:55
    
Glad I could help. Python works in mysterious ways... at least to me. I've been spending a lot of time lately hunting down errors that suddenly pop up when I run things as a script tool. Usually it's some very picky syntax like in your case. –  egdetti Oct 5 '12 at 18:16
add comment

In general you should catch exceptions. Otherwise you won't really know where things go wrong and how to handle unexpected behavior. Something else: Note that if your workspace ends with a single \ python will interpret this as a escape which will result in a invalid path.

To ensure you always have a valid workspace do:

if inws[-1:] != '\\':
    inws += '\\'

To handle exceptions:

try:
    #Set geoprocessing environments
    arcpy.env.workspace = inws
    arcpy.env.overwriteOutput = True

except:
   msg = arcpy.getmessages()
   raise Exception(msg)
   ...
share|improve this answer
add comment

The error is most probably with the last line.

arcpy.CalculateField_management(fc, field, "expression", "PYTHON")

It should be the variable expression and not the string "expression" as @PolyGeo pointed out.

share|improve this answer
1  
And on the CalculateField I am betting @user10723 will want a variable called expression rather than the string "expression" too. –  PolyGeo Oct 5 '12 at 3:20
    
@R.K., it's not true that nothing get assigned to inws: arcpy.env.workspace = inws. –  LarsVegas Oct 5 '12 at 10:16
    
Nothing gets assigned if he's running it as a standalone script. Otherwise, you're right. –  R.K. Oct 5 '12 at 10:21
    
But that's the whole point, right? To run it from the toolbox - or am I missing something essential? –  LarsVegas Oct 5 '12 at 11:01
    
I was just considering the possibility that he used a standalone script. But upon closer inspection, you're right. He was using it as tool. Missed the fact that ERROR 000539 is an ArcGIS error code. Thanks for pointing it out. –  R.K. Oct 5 '12 at 11:56
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.