<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic arcpy.CalculateField_Management syntax in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-syntax/m-p/747378#M57781</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have been struggling with a python script for several days and I am hoping someone can provide me some guidance.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;I am using Windows 10, ArcGIS 10.7.1, python 2.7.16&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The script is below.&amp;nbsp; It works correctly all the way until the final arcpy.CalculateField_management expression and I get an error that dir2 is not defined.&amp;nbsp; I have tried specifying the dir2 using single quotes, double quotes, and no quotes, and it won't work with any of those options.&lt;/P&gt;&lt;P&gt;If anyone can assist, I would greatly appreciate it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Lisa&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 10 Jan 2020 12:29:58 GMT</pubDate>
    <dc:creator>LisaDygert</dc:creator>
    <dc:date>2020-01-10T12:29:58Z</dc:date>
    <item>
      <title>arcpy.CalculateField_Management syntax</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-syntax/m-p/747378#M57781</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have been struggling with a python script for several days and I am hoping someone can provide me some guidance.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;I am using Windows 10, ArcGIS 10.7.1, python 2.7.16&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The script is below.&amp;nbsp; It works correctly all the way until the final arcpy.CalculateField_management expression and I get an error that dir2 is not defined.&amp;nbsp; I have tried specifying the dir2 using single quotes, double quotes, and no quotes, and it won't work with any of those options.&lt;/P&gt;&lt;P&gt;If anyone can assist, I would greatly appreciate it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Lisa&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Jan 2020 12:29:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-syntax/m-p/747378#M57781</guid>
      <dc:creator>LisaDygert</dc:creator>
      <dc:date>2020-01-10T12:29:58Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.CalculateField_Management syntax</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-syntax/m-p/747379#M57782</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The parameter dir2 is in refers to the expression of the calculate field. In other words, &lt;SPAN style="font-family: 'courier new', courier, monospace;"&gt;Work_order = expression&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Refer to &lt;A href="https://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/calculate-field.htm"&gt;the documentation&lt;/A&gt; for more information.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you just want it to write a hard-coded string, you have to format it with an extra set of quotes like&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;CalculateField_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fc&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"Work_order"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"'dir2'"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"PYTHON_9.3"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;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.&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;CalculateField_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fc&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"Work_order"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; dir2&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"PYTHON_9.3"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;‍&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;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&amp;nbsp;&lt;SPAN style="font-family: 'courier new', courier, monospace;"&gt;"Vista_" + dir_list2&lt;/SPAN&gt; so it should work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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&amp;nbsp;&lt;SPAN style="font-family: 'courier new', courier, monospace;"&gt;PYTHON_9.3&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: arial, helvetica, sans-serif;"&gt;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 &lt;SPAN style="font-family: 'courier new', courier, monospace;"&gt;import datetime&lt;/SPAN&gt;&amp;nbsp;library and then change line 66 to be&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;arcpy&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;CalculateField_management&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;fc&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"Date_time"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; datetime&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;datetime&lt;SPAN class="punctuation token"&gt;.&lt;/SPAN&gt;now&lt;SPAN class="punctuation token"&gt;(&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"PYTHON_9.3"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;)&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Jan 2020 15:07:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-syntax/m-p/747379#M57782</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2020-01-10T15:07:57Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.CalculateField_Management syntax</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-syntax/m-p/747380#M57783</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Blake,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As I mentioned in the original question, I tried the calculate field syntax using a variety of options, including without any quotes around dir2.&amp;nbsp; It still doesn't work.&amp;nbsp; If I use arcpy.CalculateField_management(fc, "Work_order", dir2, "PYTHON_9.3"), it still doesn't work.&amp;nbsp; The error I get from that is 'NameError: name 'Vista_1904309' is not defined'.&amp;nbsp; 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.&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Jan 2020 15:39:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-calculatefield-management-syntax/m-p/747380#M57783</guid>
      <dc:creator>LisaDygert</dc:creator>
      <dc:date>2020-01-10T15:39:20Z</dc:date>
    </item>
  </channel>
</rss>

