Select to view content in your preferred language

Script to populate file name field not working with update to 10.2.1

801
4
Jump to solution
02-14-2014 09:19 AM
ScottHawkins
Emerging Contributor
Hello everyone,

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:
import arcpy from arcpy import env  ###### ===================================================================== #### Select Attributes - add FileName field   print "Adding FileName field ... "  #### Sets new arcpy environment env.workspace  = "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):         row = tablist[cnt]         return row"""  ## Loop that adds FileName field for tables in tablist:         arcpy.AddField_management(tables, "FileName","TEXT")         arcpy.CalculateField_management(tables, "FileName","customListReplacer(cnt)","PYTHON",codeblock)         cnt+= 1



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.

Any suggestions would be GREATLY appreciated. Thanks!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Alum
You didn't say which version this did work (10.0? 10.1?)

Here's an approach that may be less dependent on how the python sessions are handled between versions....

tablist = arcpy.ListTables("","dBASE")  ## Loop that adds FileName field for table in tablist:      arcpy.AddField_management(table, "FileName", "TEXT")      expr = '"{0}"'.format(table)      arcpy.CalculateField_management(table, "FileName" , expr, "PYTHON_9.3")


(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!)

View solution in original post

0 Kudos
4 Replies
RichardFairhurst
MVP Alum
Hello everyone,

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:
import arcpy
from arcpy import env

###### =====================================================================
#### Select Attributes - add FileName field 

print "Adding FileName field ... "

#### Sets new arcpy environment
env.workspace  = "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):
        row = tablist[cnt]
        return row"""

## Loop that adds FileName field
for tables in tablist:
        arcpy.AddField_management(tables, "FileName","TEXT")
        arcpy.CalculateField_management(tables, "FileName","customListReplacer(cnt)","PYTHON",codeblock)
        cnt+= 1



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.

Any suggestions would be GREATLY appreciated. Thanks!


I don't thing the value contained by the cnt variable is actually being passed to the calculation by the expression portion.  Try:

        arcpy.CalculateField_management(tables, "FileName","customListReplacer(" + str(cnt) + ")","PYTHON",codeblock)
0 Kudos
curtvprice
MVP Alum
You didn't say which version this did work (10.0? 10.1?)

Here's an approach that may be less dependent on how the python sessions are handled between versions....

tablist = arcpy.ListTables("","dBASE")  ## Loop that adds FileName field for table in tablist:      arcpy.AddField_management(table, "FileName", "TEXT")      expr = '"{0}"'.format(table)      arcpy.CalculateField_management(table, "FileName" , expr, "PYTHON_9.3")


(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!)
0 Kudos
ScottHawkins
Emerging Contributor
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.
0 Kudos
RichardFairhurst
MVP Alum
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.


10.2 seems to be a bit more strict on enforcing Python rules for scoping variables, and SQL syntax rules.  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.
0 Kudos