Python script tool to export each feature in a feature class to its own new feature class

Here is the script:

“””
SelectAndExportFeatures.py
Selects each feature in a feature class and exports each to a new shapefile.
Created by John Speargas, 29 Jan 2012
“””

# import modules
import arcpy
from arcpy import analysis, env

# input parameters
in_fc = arcpy.GetParameterAsText(0)
field = arcpy.GetParameterAsText(1)
env.workspace = arcpy.GetParameterAsText(2)

# create search cursor
rows = arcpy.SearchCursor(in_fc)

for row in rows:
where_clause = field + “='” + row.getValue(field) + “‘”
# name the output features based on the value found in the field selected above
analysis.Select(in_fc, row.getValue(field), where_clause)

This script is set up to be added to a toolbox in ArcGIS 10. I set up the script tool parameters as shown in the dialog box below. When you select the second parameter, the field based upon which the new features should be named, in the lower part of the dialog box click next to “Obtained from” and select the input feature class parameter.

3 thoughts on “Python script tool to export each feature in a feature class to its own new feature class

  1. I would like to ask more questions about this script.
    1) first, it always errors out for me.
    2) would this be better served using the arcpy.da instead?
    3) and help/insight would be greatly appreciated!

  2. Hi, sorry — your comment was lost amongst the spam for a week. I am not familiar with arcpy.da, but since I just googled it … it looks like if arcpy.da is an option, you must be using ArcGIS 10.1? I only have 10.0, so I have not tried arcpy.da, personally. It looks like there is a Search Cursor is within the Data Access module (arcpy.da) now, but it seems the old arcpy.SearchCursor still exists as a read-only tool. That should be okay, though, because we are just reading the value in the field for each record, not modifying the attributes. In ESRI’s help, it says:

    Legacy:
    ArcGIS 10.1 added a new data access module (arcpy.da). The previously existing cursors (that are still listed under arcpy) are still functional and valid; however, the new arcpy.da cursors include significantly faster performance. In most cases, the help will illustrate the use of the arcpy.da cursors. For more information about the ‘classic’ cursor model, see the table below. (http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001q000000)

    I wish I had 10.1 so I could test it out. What error are you getting? Have you tried just hard-coding the 3 input parameters my script requires and running it directly from IDLE instead of as a tool, by any chance?

  3. Oh! Also, I just realized my indents aren’t displayed properly, and that’s crucial in Python. The “where_clause” line and the “analysis.Select” line should both be indented beneath the “for ro in rows:” line. I’ll have to format my post to fix that.

Leave a Reply

Your email address will not be published.