Creating a python list of FCs in subdirectories and append to a FC in a FGDB

435
2
06-28-2011 02:37 PM
AdamInglis
Occasional Contributor II
I need some help with the following script:

import arcpy, os, sys

# Set local variables
outLocation = r'M:\TRIM\test.gdb\trim'
emptyFC = 'troad'

try:
    fcList = []

    # All troad shp files in the workspace are placed into a list   
    def fcs_in_workspace(workspace):
      arcpy.env.workspace = workspace
      for fc in arcpy.ListFeatureClasses('troad*'):
        fcList.append(os.path.join(workspace, fc))
        
      for ws in arcpy.ListWorkspaces():
        fcs_in_workspace(os.path.join(workspace, ws))

    fcs_in_workspace(r'M:\104a\trim\shp')
    

    # Process: Append the feature classes into the empty feature class
    arcpy.Append_management(fcList, outLocation + os.sep + emptyFC, "NO_TEST")
    print fcList

except:
    # If an error occurred while running a tool print the messages
    print arcpy.GetMessages()


What it is supposed to do is build a FC list and use that list to append all the shp files to a FC in a FGDB.  The above code works for listing all the SHP files in the interactive window, but for whatever reason the  arcpy.Append_management tool only appends the first item in the list to the File geodatabase.


Anyone have any suggestions?

Thanks,

Adam
Tags (2)
2 Replies
ChrisSnyder
Regular Contributor III
You are attempting to pass an actual Python list into the Append tool. While this seems like it should work, it doesn't. You need to format the list as a big long string. For example:

fcString = ""
for fc in fcList:
   fcString = fcString + fc + ";"
arcpy.Append_management(fcString[:-1], outLocation + os.sep + emptyFC, "NO_TEST")


Another way:
arcpy.Append_management(str(fcList).replace(",",";")[1:-1], outLocation + os.sep + emptyFC, "NO_TEST")
0 Kudos
LoganPugh
Occasional Contributor III
One more way (there are probably even more):

fcString = ";".join(fcList)
arcpy.Append_management(fcString, outLocation + os.sep + emptyFC, "NO_TEST")
0 Kudos