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
Solved! Go to Solution.
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")
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!
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")
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.