0

I have a pnt shp with 50 pnts.

I want to export each one of it in a separate shapefile with arcpy writting a loop. My problem is that i want the output name i create has a different name, e.g. for 1st pnt i want the name to be pnt1_lyr for the 2nd pnt2_lyr etc. I wrote that code but i always have an error. Can someone correct me;

import arcpy
arcpy.env.workspace = "c:/Wing/Sk"
for i in range(0, 49):
    arcpy.MakeFeatureLayer_management("spatially50.shp", "pnt[i]_lyr", "FID = (i)")

2 Answers 2

1

The first option here is where you specify the beginning and ending range for the FID numbers...

import arcpy

arcpy.env.workspace = "c:/Wing/Sk"

# Set overwrite option
arcpy.env.overwriteOutput = True

# Make a Feature Layer for the original shapefile
arcpy.MakeFeatureLayer_management("spatially50.shp", "lyr_MyOriginalLayer")

# Then create a layer for each FID and export it out
for FID in range(0,49):
    arcpy.MakeFeatureLayer_management("lyr_MyOriginalLayer", "lyr_MyIndividualLayer{0}".format(FID), "\"FID\" = {0}".format(FID))
    arcpy.CopyFeatures_management("lyr_MyIndividualLayer{0}".format(FID), "zzzShapefileName {0}.shp".format(FID))
    print("Created ShapefileName {0}.shp".format(FID))

The second example is if you want to export all the FID's in the shapefile... (then you don't have to change the range...)

import arcpy

arcpy.env.workspace = "c:/Wing/Sk"

# Set overwrite option
arcpy.env.overwriteOutput = True

# Make a Feature Layer for the original shapefile
arcpy.MakeFeatureLayer_management("spatially50.shp", "lyr_MyOriginalLayer")

# Create an empty list to hold the FID numbers
myFIDNumbers = []

# First, loop through the original layer and get all the FID numbers.
with arcpy.da.SearchCursor("lyr_MyOriginalLayer", ["FID"]) as cursor:
    for row in cursor:
        myFIDNumbers.append(row[0])


# Then create a layer for each FID and export it out
for FID in myFIDNumbers:
    arcpy.MakeFeatureLayer_management("lyr_MyOriginalLayer", "lyr_MyIndividualLayer{0}".format(FID), "\"FID\" = {0}".format(FID))
    arcpy.CopyFeatures_management("lyr_MyIndividualLayer{0}".format(FID), "zzzShapefileName {0}.shp".format(FID))
    print("Created ShapefileName {0}.shp".format(FID))
0
1

This will take the first shape in the dataframe and make Layers (temporary) and save them as shapefiles with different names like you wanted. Use the code in the Python window.

import arcpy, os
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "")[0]
lyr = arcpy.mapping.ListLayers(mxd, "", df)[0]

desc = arcpy.Describe(lyr)
for i in range(0,49):
    out = arcpy.MakeFeatureLayer_management(lyr, desc.basename+str(i)+"temp.shp",""""FID" = """ + str(i))
    arcpy.CopyFeatures_management(out, desc.path+os.sep +desc.basename+str(i)+".shp")

You can replace the last line with the following line if you need pnt1... as name of the file.

arcpy.CopyFeatures_management(out, desc.path+os.sep +"pnt"+str(i+1)+".shp")
2
  • thank you very much ElikeMike, but when i runned it created 5 pnt shpaefiles with attribyte tables which were containig all the data.
    – Konstantin
    Commented Mar 18, 2015 at 15:44
  • Please try it again, I added the missing line.
    – EikeMike
    Commented Mar 18, 2015 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.