Using selection of features in ArcMap in Python script?

arcpy.CopyFeatures_management works like right-clicking on the layer > export data > selected features. Then you can make the layer from that exported selection. I'd do this "in_memory" so you don't have deal with overwriting each time you run the script.

import arcpy
from arcpy import env

arcpy.env.workspace = "in_memory"

selected_features = "The Feature Class with the selection.shp"
pipes = "the new feature class to convert to a layer"

#this will create a new feature class from the selected features but will do it In Memory
arcpy.CopyFeatures_management (selected_features, pipes)

#Now do all the other stuff you want like convert it to a layer and work with it

arcpy.MakeFeatureLayer_management(pipes,"pipeslyr")

The selection would need to be made before this is run.

Tags:

Arcpy

Arcmap