1

I have a small script that should take the name of the file and add it to a newly created field in several shapefiles.

import arcpy
arcpy.env.workspace = r"c:\users\athom\desktop\timber\New4"
arcpy.env.overwriteOutput = True
fcs = arcpy.ListFeatureClasses()
for fc in fcs:
 field = 'NAME'
 expression = str(fc)
 print expression
 arcpy.AddField_management(fc, field, "TEXT")
 arcpy.CalculateField_management(fc, field, "expression", "PYTHON")
del expression
del fc
del fcs

However, the file name of the first feature is written into the attribute table for each following features even though it shows in the python interpreter as the correct file name as "expression" - the attribute for NAME ends up being SA14107.shp in each shapefile.

python output

Why is the expression not being overwritten?

asked Nov 6, 2015 at 19:27

1 Answer 1

3

You need to pass the expression as a quoted string, like this:

arcpy.CalculateField_management(fc, field, "'" + expression + "'" , "PYTHON")
answered Nov 6, 2015 at 20:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.