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.

So I previously successfully used the sample code here: Model Builder - How to calculate Field using part of filename

# Import standard library modules
import arcpy, os, sys
from arcpy import env

# Allow for file overwrite
arcpy.env.overwriteOutput = True

# Set the workspace directory 
env.workspace = r"C:\temp.gdb" 

# Get the list of the featureclasses to process
fc_tables = arcpy.ListFeatureClasses()

# Loop through each file and perform the processing
for fc in fc_tables:
    print str("processing " + fc)

    # Define field name and expression
    field = "SID"
    expression = str(fc[:5]) #subsets first 5 characters of fc name

    # Create a new field with a new name
    arcpy.AddField_management(fc,field,"TEXT")

    # Calculate field here
    arcpy.CalculateField_management(fc, field, "expression", "PYTHON") 

But now I'm running it again, with no changes, even with the same datasets I used before, and it no longer works. I cannot see how or why it errors out.

Error message:

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

Help!?!

share|improve this question
add comment

2 Answers

Instead of:

expression = str(fc[:5])

Try:

expression = '"' + str(fc[:5]) + '"'

Explanation:

  • The contents of expression is evaluated by a second Python interpreter within the Calculate Field tool. Thus anything you want to be interpreted as a string literal needs to be enclosed with quotes.
share|improve this answer
    
+1 for an explanation! I never really understood that. Makes sense. –  egdetti Mar 28 '13 at 17:34
add comment

Take the quotes completely off in the calculate field function:

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

It's a variable, not a string. It stores the string you already set when doing

expression = str(fc[:5])

See example 2 in the ArcGIS Help docs and you'll see how they set an expression to a variable and passes it to the Calculate Field function. No quotes.

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000004m000000

EDIT:

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

should be

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

This will add double quotes around your string, which should work out for you.

share|improve this answer
    
Thanks for the quick response! Unfortunately then I get this error: Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000539: Error running expression: ten_m <type 'exceptions.NameError'>: name 'ten_m' is not defined Failed to execute (CalculateField). –  J Louie Mar 28 '13 at 17:22
    
so basically it took the filename for the first feature class and is saying it's not defined. –  J Louie Mar 28 '13 at 17:25
    
This is not correct in this situation. Calculate Field has its own Python interpreter and that interpreter will evaluate the contents of the expression argument, which is assigned by the outer interpreter. So by that time, str(fc[:5]) has already been evaluated into a string. If you do not wrap that string in quotes, it will be interpreted by the inner interpreter as an identifier, not a string literal. That is why the quotes are necessary. –  blah238 Mar 28 '13 at 17:58
    
@blah238 Thank you so much!!! This did the trick! I am still absolutely baffled as to why the original code worked, a number of times, quite literally last week! @Mike Just to clarify, I DID have to remove the quotes from arcpy.CalculateField_management(fc, field, expression, "PYTHON") When I left the quotes around "expression", it gave me the same undefined error as I originally posted. –  user16616 Mar 28 '13 at 18:04
    
Ah good point. I went back and looked at the syntax in the help menu forgot that the expression still needs to be wrapped in quotes using the Python parser, regardless if it's a string or not. –  Mike Mar 28 '13 at 18:10
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.