<?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: Script to populate file name field not working with update to 10.2.1 in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/script-to-populate-file-name-field-not-working/m-p/282985#M21855</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You didn't say which version this did work (10.0? 10.1?) &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's an approach that may be less dependent on how the python sessions are handled between versions....&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;tablist = arcpy.ListTables("","dBASE")&amp;nbsp; ## Loop that adds FileName field for table in tablist: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(table, "FileName", "TEXT") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; expr = '"{0}"'.format(table) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(table, "FileName" , expr, "PYTHON_9.3")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;(PYTHON_9.3 is both most efficient and functional, it really should be labeled "PYTHON_9.3_and_later" but that was probably too long. It should be the default IMHO, but that would probably break a lot of scripts!)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 14 Feb 2014 18:23:17 GMT</pubDate>
    <dc:creator>curtvprice</dc:creator>
    <dc:date>2014-02-14T18:23:17Z</dc:date>
    <item>
      <title>Script to populate file name field not working with update to 10.2.1</title>
      <link>https://community.esri.com/t5/python-questions/script-to-populate-file-name-field-not-working/m-p/282983#M21853</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello everyone, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I had a script that added a field and populated it with the file name. In this case the files are .DBF. The only thing that has changed is our update to 10.2.1. Here is the code that previously worked:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy from arcpy import env&amp;nbsp; ###### ===================================================================== #### Select Attributes - add FileName field&amp;nbsp;&amp;nbsp; print "Adding FileName field ... "&amp;nbsp; #### Sets new arcpy environment env.workspace&amp;nbsp; = "C:\\directory" tablist = arcpy.ListTables("","dBASE") print 'tablist:',tablist # counter for appending filename to DBF cnt = 0&amp;nbsp; # codeblock for CalculateField function codeblock = """ def customListReplacer(cnt): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row = tablist[cnt] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return row"""&amp;nbsp; ## Loop that adds FileName field for tables in tablist: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(tables, "FileName","TEXT") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(tables, "FileName","customListReplacer(cnt)","PYTHON",codeblock) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt+= 1&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is the problem: previously the 'cnt' read through the 'tablist' and populated the dbf with the correct file name. Now, this script populates the FileName field with the first DBF in the directory. As a result, every DBF has the first DBF listed in the FileName field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any suggestions would be GREATLY appreciated. Thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 14 Feb 2014 17:19:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-to-populate-file-name-field-not-working/m-p/282983#M21853</guid>
      <dc:creator>ScottHawkins</dc:creator>
      <dc:date>2014-02-14T17:19:36Z</dc:date>
    </item>
    <item>
      <title>Re: Script to populate file name field not working with update to 10.2.1</title>
      <link>https://community.esri.com/t5/python-questions/script-to-populate-file-name-field-not-working/m-p/282984#M21854</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hello everyone, &lt;BR /&gt;&lt;BR /&gt;I had a script that added a field and populated it with the file name. In this case the files are .DBF. The only thing that has changed is our update to 10.2.1. Here is the code that previously worked:&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
from arcpy import env

###### =====================================================================
#### Select Attributes - add FileName field 

print "Adding FileName field ... "

#### Sets new arcpy environment
env.workspace&amp;nbsp; = "C:\\directory"
tablist = arcpy.ListTables("","dBASE")
print 'tablist:',tablist
# counter for appending filename to DBF
cnt = 0

# codeblock for CalculateField function
codeblock = """
def customListReplacer(cnt):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row = tablist[cnt]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return row"""

## Loop that adds FileName field
for tables in tablist:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(tables, "FileName","TEXT")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(tables, "FileName","customListReplacer(cnt)","PYTHON",codeblock)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cnt+= 1
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Here is the problem: previously the 'cnt' read through the 'tablist' and populated the dbf with the correct file name. Now, this script populates the FileName field with the first DBF in the directory. As a result, every DBF has the first DBF listed in the FileName field.&lt;BR /&gt;&lt;BR /&gt;Any suggestions would be GREATLY appreciated. Thanks!&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I don't thing the value contained by the cnt variable is actually being passed to the calculation by the expression portion.&amp;nbsp; Try:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(tables, "FileName","customListReplacer(" + str(cnt) + ")","PYTHON",codeblock)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:42:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-to-populate-file-name-field-not-working/m-p/282984#M21854</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2021-12-11T13:42:16Z</dc:date>
    </item>
    <item>
      <title>Re: Script to populate file name field not working with update to 10.2.1</title>
      <link>https://community.esri.com/t5/python-questions/script-to-populate-file-name-field-not-working/m-p/282985#M21855</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You didn't say which version this did work (10.0? 10.1?) &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's an approach that may be less dependent on how the python sessions are handled between versions....&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;tablist = arcpy.ListTables("","dBASE")&amp;nbsp; ## Loop that adds FileName field for table in tablist: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(table, "FileName", "TEXT") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; expr = '"{0}"'.format(table) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(table, "FileName" , expr, "PYTHON_9.3")&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;(PYTHON_9.3 is both most efficient and functional, it really should be labeled "PYTHON_9.3_and_later" but that was probably too long. It should be the default IMHO, but that would probably break a lot of scripts!)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 14 Feb 2014 18:23:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-to-populate-file-name-field-not-working/m-p/282985#M21855</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2014-02-14T18:23:17Z</dc:date>
    </item>
    <item>
      <title>Re: Script to populate file name field not working with update to 10.2.1</title>
      <link>https://community.esri.com/t5/python-questions/script-to-populate-file-name-field-not-working/m-p/282986#M21856</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you rfairhur24 and curtvprice! Both of your solutions worked! My previous version was 10.1. Not sure why this code previously worked. I appreciate the responses.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 14 Feb 2014 18:36:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-to-populate-file-name-field-not-working/m-p/282986#M21856</guid>
      <dc:creator>ScottHawkins</dc:creator>
      <dc:date>2014-02-14T18:36:07Z</dc:date>
    </item>
    <item>
      <title>Re: Script to populate file name field not working with update to 10.2.1</title>
      <link>https://community.esri.com/t5/python-questions/script-to-populate-file-name-field-not-working/m-p/282987#M21857</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Thank you rfairhur24 and curtvprice! Both of your solutions worked! My previous version was 10.1. Not sure why this code previously worked. I appreciate the responses.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;10.2 seems to be a bit more strict on enforcing Python rules for scoping variables, and SQL syntax rules.&amp;nbsp; When I went to 10.2 several of my scripts started to fail due to sloppy editing of some SQL where I didn't fully transition from the field delimiters of an unjoined table to the field delimiters of a joined table when I decided to change that aspect of the script. 10.1 forgave the unjoined delimiters with a joined field, but 10.2 didn't.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 14 Feb 2014 19:00:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-to-populate-file-name-field-not-working/m-p/282987#M21857</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2014-02-14T19:00:03Z</dc:date>
    </item>
  </channel>
</rss>

