Select to view content in your preferred language

Field calculation in Python - expression not defined

4827
2
Jump to solution
10-01-2013 08:00 PM
MichaelGresham
New Contributor
Hi,

Apologies if something similar has been asked before in a previous post. I'm trying to add a field and then calculate it with the name of the file. The python script I'm trying to run below seems to run okay however it falls over at the calculate field step of the process.

import os, arcpy

Input = "C:\\GIS\\TempFile.shp"
expression = os.path.basename(Input).rstrip(os.path.splitext(Input)[1])
print Input
print expression

# Add field called "Filename"
arcpy.AddField_management("TempFile","Filename","TEXT",254)

# Calculate field with file name
arcpy.CalculateField_management(Input, "Filename", expression, "PYTHON")


The error that comes up is:

ERROR 000539: Error running expression: expression
Traceback (most recent call last):
  File "<expression>", line 1, in <module>
NameError: name 'expression' is not defined

However I find this confusing as I would've thought the line below defines "expression".

expression = os.path.basename(Input).rstrip(os.path.splitext(Input)[1])

I feel I'm pretty close to getting it however I need a helping hand from somewhere. Any feedback would be welcomed.

Regards,
Mike
0 Kudos
1 Solution

Accepted Solutions
KimOllivier
Honored Contributor
You have to quote the expression in single quotes in the style of an SQL query because that is what it is.
arcpy.CalculateField_management(Input, "Filename","'"+ expression+"'", "PYTHON")


To debug the expressions run the tool interactively, then grab a Python snippet from the results panel and paste it into your script.
This will show you some working code that you can then edit to replace parts with a variable.

In a script it is actually easier to use a cursor than the CalculateField which is really only designed for Modelbuilder.
A cursor is just as fast, can be much easier to write and debug and can handle errors such as null values with an if statement.

(Untested pseudocode)
with arcpy.da.UpdateCursor(Input,['filename']) as cur:
    for row in cur:
        row[0] = expression
        cur.updateRow(row)


No gymnastics needed to quote strings. The with closes the cursor.

View solution in original post

0 Kudos
2 Replies
KimOllivier
Honored Contributor
You have to quote the expression in single quotes in the style of an SQL query because that is what it is.
arcpy.CalculateField_management(Input, "Filename","'"+ expression+"'", "PYTHON")


To debug the expressions run the tool interactively, then grab a Python snippet from the results panel and paste it into your script.
This will show you some working code that you can then edit to replace parts with a variable.

In a script it is actually easier to use a cursor than the CalculateField which is really only designed for Modelbuilder.
A cursor is just as fast, can be much easier to write and debug and can handle errors such as null values with an if statement.

(Untested pseudocode)
with arcpy.da.UpdateCursor(Input,['filename']) as cur:
    for row in cur:
        row[0] = expression
        cur.updateRow(row)


No gymnastics needed to quote strings. The with closes the cursor.
0 Kudos
MichaelGresham
New Contributor
Hi Kim,

I hope all is well with yourself back in NZ. Thanks for that - both sets of code worked for this problem. I will have to explore the arcpy.da cursor functions a bit more to find out what they offer instead of the standard python snippets from modelbuilder :).

Regards,
Mike
0 Kudos