Arcpy Calculate Field error

769
6
01-31-2014 05:51 AM
ZackBartlett
New Contributor III
Hi all,

I have a simple script that adds a field to a set of feature classes

for fc in fcList:
        arcpy.AddField_management(fc,Source,FieldType2,fieldLength)
        arcpy.AddMessage("Fields added...")


In the above example: Source, FieldType2, and fieldLength all refer back to variables containing the field name (sourcefile), the field type (text)
and field Length (75)

For the next step I want to cycle through the feature classes again. This time, populating the sourcefile field with the feature class name; in this case, it would be fc.

for fc in fcList:
        arcpy.CalculateField_management(fc,Source,"fc","PYTHON")


The first code snippet runs without error. The text field of length 75 is added.

The second code snippet gives the following error:

Traceback (most recent call last):
File "T:\JOBS\123110494\gis_data\spatial_data\project_discipline\modeling\scripts\HWard\add_calc_dissolve.py", line 242, in <module>
arcpy.CalculateField_management(fc,Source,"fc","PYTHON")
File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\management.py", line 3129, in CalculateField
raise e
ExecuteError: ERROR 000539: Error running expression: fc
Traceback (most recent call last):
File "<expression>", line 1, in <module>
NameError: name 'fc' is not defined

Failed to execute (CalculateField).


Failed to execute (master).


I've tried putting single quotes around fc ('fc'), tried it without quotes (fc), and even tried converting it to a string (str(fc)), but nothing seems to work.

I've also tried replacing "fc" with "test", and I get the same error, but in that case, it tells me "test" does not exist.

Any thoughts would be greatly appreciated.

Thanks in advance

-Z
Tags (2)
0 Kudos
6 Replies
DanPatterson_Retired
MVP Emeritus
show your full script, it looks like it is losing reference to your feature class list
0 Kudos
ZackBartlett
New Contributor III
I've uploaded the full script as an attachment. For simplicity in my original post, I removed the other calculate field functions. They were working fine, anyway.

The problematic code is at line 242.

Thanks
0 Kudos
AnthonyGiles
Frequent Contributor
Zack,

I have a toolbox that takes a workspace of feature classes
and adds a column to each feature class (called orgFilNam 'Original File
Name')and populates each feature within each feature class with its
filename.

It sound similar to what you are after, if you send me a private message with your email address I will forward it on to you. I can't upload it to this post at the moment as I am only on my iPad

Regards

Anthony
0 Kudos
MichaelVolz
Esteemed Contributor
Zack:

Does your script work for the shapefile but not the geodatabase.

For the shapefile you have:

arcpy.CalculateField_management(fc,Source,str(fc), "PYTHON")


For the geodatabase you have:

arcpy.CalculateField_management(fc,Source,"fc","PYTHON")


Why is this different?
0 Kudos
ZackBartlett
New Contributor III
I have not tried running the script on shapefiles, so I'm not sure if this works. But I have tried using the str(fc) code in the geodatabase file. Still no luck.
0 Kudos
MichaelVolz
Esteemed Contributor
Can you add a print statement for str(fc) to see exactly what that is returning just before using it in the CalculateField_management statement?  That might provide a clue to your problem.
0 Kudos