Iterate tables through Python

3971
3
Jump to solution
06-15-2015 09:16 AM
MeredithGreen
New Contributor III

I have a script listed below:

tables = arcpy.ListTables("res*")

for table in tables:

    res_500yrsumm = os.path.join(output_workspace, str("res_500yrsumm"))

    res_summ = os.path.join(output_workspace, str("res_summ"))

    arcpy.CopyRows_management(res_500yrsumm, res_summ)

    arcpy.DeleteRows_management(res_summ)

    arcpy.Append_management(table, res_summ, "NO_TEST", "", "")

There are seven tables it is suppose to iterate through and append to res_summ but for some reason it only does one table and stops.  I appended all of the tables separately in ArcMap using the append tool to make sure there were no issues with fields matching and it worked fine.  Is there an error in my script I'm not seeing?  Any help would be greatly appreciated.

0 Kudos
1 Solution

Accepted Solutions
SepheFox
Frequent Contributor

Hi Meredith, It looks like you are creating your res_summ table anew for each iteration, overwriting the previous one each time. Try taking the copy/delete rows outside of the loop.

View solution in original post

3 Replies
SepheFox
Frequent Contributor

Hi Meredith, It looks like you are creating your res_summ table anew for each iteration, overwriting the previous one each time. Try taking the copy/delete rows outside of the loop.

DarrenWiens2
MVP Honored Contributor

Since you've already made a list of tables (the input tables argument for Append can be a list), you can use that in Append. No loop required.

tables = arcpy.ListTables("res*")
res_summ = os.path.join(output_workspace, str("res_summ"))
arcpy.Append_management(tables, res_summ, "NO_TEST", "", "")
MeredithGreen
New Contributor III

Using the suggestions I adjusted the scrip to look like this:

res_500yrsumm = os.path.join(output_workspace, str("res_500yrsumm"))

summ_res = os.path.join(output_workspace, str("summ_res"))

arcpy.CopyRows_management(res_500yrsumm, summ_res)

arcpy.DeleteRows_management(summ_res)

tables = arcpy.ListTables("res*")

for table in tables:

    arcpy.Append_management(table, summ_res, "NO_TEST", "", "")

And everything is doing what it is suppose to be doing.  Thanks for the tips!