Hello 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 to have a different name, e.g. for 1st pnt i want the output layers' name to be pnt1_lyr, for the 2nd point, 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")
Thank you very much
Solved! Go to Solution.
Untested, but should be something like:
import arcpy from arcpy import env env.workspace = "c:/Wing/Sk" for i in range(50): arcpy.FeatureClassToFeatureClass_conversion("spatially50", "c:/Wing/Sk", "pnt" + str(i), "FID=" + str(i))
If you are needing to make a new shapefile, then you are using the wrong geoprocessing tool. Make Feature Layer creates a layer in memory for use in a map document, not a new shapefile. Try using Feature Class to Feature Class tool, this can create new shapefiles on your hard drive.
Also if you are having an error in your code, please post the error so it helps us understand what is going wrong.
oh, i thought that i would create a shepfile form an attribute od an another shapefile i should use make new feature layer. Although i knew i would have it only in memore and not in hard drive. Ok i propably must do that as you decribe thank you.
First, I don't think MakeFeatureLayer_management is what you want unless you are using it as an intermediate step before exporting the feature layer.
Second, it helps to paste specific error messages.
Third, what about putting str(i) in place of i in the layer name you are concatenating.
For the Make Feature Layer you are right, thank you both. I tried it with str(i) and i have again the same message that i cannot use it for FID = i!
This is it:
Runtime error Traceback (most recent call last): File "<string>", line 4, in <module> File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 6043, in MakeFeatureLayer raise e ExecuteError: ERROR 000230: Failed selecting with FID = i Failed to execute (MakeFeatureLayer)
Just to pile on, your where clause "FID = i" literally translates to "where FID = the letter i" (well, technically the number i because it's not within quotes). There are A LOT of discussions on here about how to make a where clause incorporating a variable, and where to put the quotation marks to make it function properly, but I believe it should be as simple as: "FID = " + str(i).
Oh ,so it's not totally wrong?
Well, it is wrong in the sense that you're not using as a variable (values 0-50), you're using it always as the value 'i'.
Ok i tried that with feature class to feature class you are right this is what i e=wanted. But i still have the same problem. How can i create a loop for 50 pnts for that? I am posting you my new code
import arcpy
from arcpy import env
env.workspace = "c:/Wing/Sk"
arcpy.FeatureClassToFeatureClass_conversion("spatially50", "c:/Wing/Sk", "pnt1", "FID=0")
Untested, but should be something like:
import arcpy from arcpy import env env.workspace = "c:/Wing/Sk" for i in range(50): arcpy.FeatureClassToFeatureClass_conversion("spatially50", "c:/Wing/Sk", "pnt" + str(i), "FID=" + str(i))