I am trying to make a selection of my "layer" and create a "new_layer" of the selection. Next I want to append data to the memory layer. But nothing get appended with this script, nor get I any messages. After the data is appended I will write it to a permanent layer. Can I append data to a memory layer like I am trying to do or what seem to be wrong?
try:
# Make memory layer from subset of feature class
new_layer = arcpy.MakeFeatureLayer_management("layer","sub_layer","Contract = AB23" )
except:
print(arcpy.GetMessages())
DataToAppend = "c:/data.gdb/MyLayer"
appendData = []
appendData.append(DataToAppend)
# Append data to layer from input
arcpy.Append_management(
inputs=appendData,
target=new_layer,
schema_type="NO_TEST",
)
# save layer permanently
arcpy.CopyFeatures_management("sub_layer", out_location)
-----------------------EDIT---------------------------
This script work. But its feels a bit bloated and I am need to delete the copied feature class if appendData fails somehow. I will make this dynamic and add it as a script to a toolbox
# Execute CopyFeatures to make a new copy of the feature class
arcpy.CopyFeatures_management(inFeatures, outFeatures)
# Create temporary layer based on the copied FC
arcpy.MakeFeatureLayer_management(outFeatures, "tempLayer")
print("Created feature layer")
# Select Subset of temp layer
arcpy.SelectLayerByAttribute_management("tempLayer", "NEW_SELECTION", "Contract IN ('AB123')")
if int(arcpy.GetCount_management("tempLayer")[0]) > 0:
print("Selected items")
arcpy.DeleteFeatures_management("tempLayer")
new_data = c:/data.gdb/MyLayer
appendData = []
appendData.append(new_data)
# Append data to layer from input
arcpy.Append_management(
inputs=appendData,
target=outFeatures,
schema_type="NO_TEST",
)