<?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 Re: add and calculate field in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/add-and-calculate-field/m-p/657336#M51138</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you, mnakleh, that works great! Thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 30 Sep 2011 16:10:29 GMT</pubDate>
    <dc:creator>YinghaiKe</dc:creator>
    <dc:date>2011-09-30T16:10:29Z</dc:date>
    <item>
      <title>add and calculate field</title>
      <link>https://community.esri.com/t5/python-questions/add-and-calculate-field/m-p/657333#M51135</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I want to add a field "filename" with "TEXT" format for a shape file and assign this field with the file name. The shape file only has one record. Here is my code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;from arcpy import env&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;# Set environment settings&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;env.workspace = "D:/Workspace"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;fc = "01055p.shp"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;tmp = fcname.split('.')&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;tmp2 = tmp[0]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.AddField_management(fc, "filename", "TEXT", "", "", 25)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.CalculateField_management(fc, "filename", tmp2)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But I got error saying "CalculateField_management" failed to execute. Is there any problem with the script? Thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Sep 2011 22:40:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-and-calculate-field/m-p/657333#M51135</guid>
      <dc:creator>YinghaiKe</dc:creator>
      <dc:date>2011-09-29T22:40:59Z</dc:date>
    </item>
    <item>
      <title>Re: add and calculate field</title>
      <link>https://community.esri.com/t5/python-questions/add-and-calculate-field/m-p/657334#M51136</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;What is &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;&lt;STRONG&gt;fcname&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN&gt;? You don't specify it before calling it. Although, that isn't directly related to your error.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You could try adding:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;arcpy.AddMessage('%s' % tmp2)&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;just before your add field line, to check what tmp2 is actually coming out like before the calculation.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 30 Sep 2011 05:32:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-and-calculate-field/m-p/657334#M51136</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2011-09-30T05:32:59Z</dc:date>
    </item>
    <item>
      <title>Re: add and calculate field</title>
      <link>https://community.esri.com/t5/python-questions/add-and-calculate-field/m-p/657335#M51137</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you're creating the textfield &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;filename&lt;/SPAN&gt;&lt;SPAN&gt;, and trying to write text to it, I think I see the problem.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;For string literals, your expression value needs to be encapsulated by quotes when it's passed to the Field Calculator. Your code should therefore be:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;arcpy.CalculateField_management(fc, "filename", '"' + tmp2 + '"')&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What you were passing to the expression field before was this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;myfilename&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;whereas what you &lt;/SPAN&gt;&lt;STRONG&gt;wanted&lt;/STRONG&gt;&lt;SPAN&gt; to send, and what you'd see in the Field Calculator, is this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;"myfilename"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope this helps!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 30 Sep 2011 12:07:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-and-calculate-field/m-p/657335#M51137</guid>
      <dc:creator>MarcNakleh</dc:creator>
      <dc:date>2011-09-30T12:07:43Z</dc:date>
    </item>
    <item>
      <title>Re: add and calculate field</title>
      <link>https://community.esri.com/t5/python-questions/add-and-calculate-field/m-p/657336#M51138</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you, mnakleh, that works great! Thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 30 Sep 2011 16:10:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/add-and-calculate-field/m-p/657336#M51138</guid>
      <dc:creator>YinghaiKe</dc:creator>
      <dc:date>2011-09-30T16:10:29Z</dc:date>
    </item>
  </channel>
</rss>

