arcpy.CalculateField_Management syntax

1533
2
01-10-2020 04:29 AM
LisaDygert
Occasional Contributor

I have been struggling with a python script for several days and I am hoping someone can provide me some guidance.

I need to be able to enter the filename from a txt file (original data) into the mdb table that was created from the txt file into a field named work_order.

I am using Windows 10, ArcGIS 10.7.1, python 2.7.16

The script is below.  It works correctly all the way until the final arcpy.CalculateField_management expression and I get an error that dir2 is not defined.  I have tried specifying the dir2 using single quotes, double quotes, and no quotes, and it won't work with any of those options.

If anyone can assist, I would greatly appreciate it.

Lisa

Tags (1)
0 Kudos
2 Replies
BlakeTerhune
MVP Regular Contributor

The parameter dir2 is in refers to the expression of the calculate field. In other words, Work_order = expression

Refer to the documentation for more information.

If you just want it to write a hard-coded string, you have to format it with an extra set of quotes like

arcpy.CalculateField_management(fc, "Work_order", "'dir2'", "PYTHON_9.3")

That will just write the string dir2 in the Work_order field of every affected record. If, however, you want to write the value of the variable dir2, then you should be able to just remove all the quotes.

arcpy.CalculateField_management(fc, "Work_order", dir2, "PYTHON_9.3")

Just ensure that the value of the dir2 variable matches the datatype of the Work_order field. In your case, I see you're defining dir2 as a string "Vista_" + dir_list2 so it should work.

Also, I noticed you are importing string, shutil, and sys but I don't see them actually used anywhere. You should keep that clean and remove imported libraries you don't use. I also recommend being consistent and using all Python or all VB for your calculate field expression type. Python is the more supported language so stick with PYTHON_9.3

So if you want to calculate the current date/time in your Date_time field (assuming the field is actually a date data type), you should import datetime library and then change line 66 to be

arcpy.CalculateField_management(fc, "Date_time", datetime.datetime.now(), "PYTHON_9.3")
LisaDygert
Occasional Contributor

Hi Blake,

As I mentioned in the original question, I tried the calculate field syntax using a variety of options, including without any quotes around dir2.  It still doesn't work.  If I use arcpy.CalculateField_management(fc, "Work_order", dir2, "PYTHON_9.3"), it still doesn't work.  The error I get from that is 'NameError: name 'Vista_1904309' is not defined'.  So, it calculates the value correctly, as that is what I am expecting, but it doesn't populate the field and give the error above.

The code sample I included is actually a subset of a much larger script, so the other imports are from other commands not included in the small subset I included for the purpose of the question.  

0 Kudos