The script I use for select_analysis is no longer working. I believe part of the problem is the source data I use (and I'm not the administrator of) has changed the field properties of the attribute I use in my where_clause to string.

What I need my script to do is extract each polygon and export it to another shapefile with a naming convention that includes the where_clause field as an identifier. I use the script when the input feature has 5 or more polygons I need to extract and can be as many as 35. Also the criteria for the where_clause is numeric but not necessarily in numerical order every time. See example script below.

Right now I get the following script...

import arcpy, os

TRACT = "T407"

input_shapefile = r"S:\NRCS\HEL\Workspace\Shapefiles\T407_Flds.shp"
output_shapefile = r"S:\NRCS\HEL\Workspace\Shapefiles"

arcpy.Select_analysis('(input_shapefile)', '(output_shapefile) + (TRACT) + "_Fld5.shp"', '"clu_number" = 5')
arcpy.Select_analysis('(input_shapefile)', '(output_shapefile) + (TRACT) + "_Fld6.shp"', '"clu_number" = 6')
arcpy.Select_analysis('(input_shapefile)', '(output_shapefile) + (TRACT) + "_Fld7.shp"', '"clu_number" = 7')
arcpy.Select_analysis('(input_shapefile)', '(output_shapefile) + (TRACT) + "_Fld8.shp"', '"clu_number" = 8')
arcpy.Select_analysis('(input_shapefile)', '(output_shapefile) + (TRACT) + "_Fld16.shp"', '"clu_number" = 16')
arcpy.Select_analysis('(input_shapefile)', '(output_shapefile) + (TRACT) + "_Fld17.shp"', '"clu_number" = 17')
arcpy.Select_analysis('(input_shapefile)', '(output_shapefile) + (TRACT) + "_Fld18.shp"', '"clu_number" = 18')
arcpy.Select_analysis('(input_shapefile)', '(output_shapefile) + (TRACT) + "_Fld26.shp"', '"clu_number" = 26')
arcpy.Select_analysis('(input_shapefile)', '(output_shapefile) + (TRACT) + "_Fld38.shp"', '"clu_number" = 38')

And get the following error...

Traceback (most recent call last):
  File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\analysis.py", line 90, in Select
    raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input Features: Dataset (input_shapefile) does not exist or is not supported
Failed to execute (Select).

So I tried using the Select tool in ArcMap and it worked extracting a single polygon, but when I copy the script and paste it to the script it doesn't like the where_clause.

Script used...

import arcpy, os

arcpy.Select_analysis(in_features="S:/NRCS/HEL/Workspace/Shapefiles/T407_Flds.shp", out_feature_class="S:/NRCS/HEL/Workspace/Shapefiles/T407_Fld5.shp", where_clause='"clu_number" = '5'')

Error Dialogue Box

There is an error in your program: invalid syntax

If I mess with the quotes around the identifier for the where_clause I get it to run but get an empty shapefile since nothing matches the clause.

Also my file paths might be part of the error, but I'm not sure. All I know about writing scripts is what I've read on the internet and it took me a long time just to get this far. Being able to loop this process would be a great time saver and why I have devoted so much time to trying to figure it out.

  • Try where_clause=' "clu_number" = 5' instead?
    – Hornbydd
    Oct 25, 2019 at 19:31
  • Do you want all of the unique values of clu_number or just these 9 values? If the former, check out SplitByAttributes()
    – SMiller
    Oct 28, 2019 at 1:27

2 Answers 2


Good start. Here are a few comments and adjustments to your code. If all of the unique values of clu_number will be used, you could instead use SplitByAttributes(); I've assumed that there are some other clu_number values that are not being exported, and instead used a list for the desired values.

The ERROR 000732: Input Features: Dataset (input_shapefile) is occurring because the Python variable input_shapefile has been enclosed in parentheses and quotes, forming a new string. Just use input_shapefile here.

In your second set of code the error is likely an issue with the where_clause. You can wrap the entire clause in triple-quotes """ which may help avoid quote issues. I suspect the clu_number field is also numeric, in which case you'd need to remove the single quotes around the value.

I made a separate line for the whereclause for readability, but it can be included in the Select_analysis parameters instead. The AddFieldDelimiters() allows you to not have to know for certain whether the underlying shapefile or geodatabase feature class requires quotes or other delimiters around the field name. In this I assumed that your clu_number field was numeric. If it is a string, you need to enclose the placeholder {} in single quotes """{} = '{}'""".

String concatenation can be tricky and is often slower than the built in .format method. I used placeholders within the string and .format throughout the code below to more easily insert values into the string. One issue that you would encounter with the code as written is that the output would be written to the r"S:\NRCS\HEL\Workspace" folder with the name "ShapefilesT407_Fld6.shp" rather than in the Shapefiles folder. Two ways to get it in the right folder would be to use os.path.join() instead of string concatenation of these paths, or to set the arcpy.env.workspace

import arcpy, os # you imported os module but didn't use it. I've added an example.

tract = "T407" # change this to a lowercase value (Python formatting; caps usually reserved for constants.)  Since it's defined here, we can use later on.
clu_values = [5, 6, 7, 8, 16, 17, 18, 26, 38] # created list of values for the where clause and output feature class name

# input_shapefile = r"S:\NRCS\HEL\Workspace\Shapefiles\T407_Flds.shp"
# changed the input shapefile to just be the filename, because it will be accessed from the env.workspace. 
#output_shapefile = r"S:\NRCS\HEL\Workspace\Shapefiles" 
# Changed to use a workspace so that you don't have to specify the output folder each time.
output_folder = r"S:\NRCS\HEL\Workspace\Shapefiles" 
arcpy.env.workspace = output_folder
input_shapefile = "{}_Flds.shp".format(tract) #(If this doesn't work as described, use `input_shapefile = os.path.join(output_folder, "{}_Flds.shp".format(tract)

#arcpy.Select_analysis('(input_shapefile)', '(output_shapefile) + (TRACT) + "_Fld5.shp"', '"clu_number" = 5') #You already have the input shape file defined as a string value, you don't need to enter it again here -- and don't need to surround with parenthesis in the string.

# loop through the clu_values list and perform the selection.
for val in clu_values:
    output_shapefile = "{}_Fld{}".format(tract, val)
    whereclause = """{} = {}""".format(arcpy.AddFieldDelimiters(input_shapefile, "clu_number"),val)
    arcpy.Select_analysis(input_shapefile, output_shapefile, whereclause)
  • First off thank you for your time. I have looked at SplitbyAttributtes() in the past as it would be perfect except for I get a table as an ouput when I need shapefiles. Also I need to have the naming convention as specified above for the output. Not sure if there is a way to change the output a feature class rather than a table. So, I used the script you provided and had to add quotes around the clu_number because it is a string. Worked beautifully. Thanks!
    – Elkins 87
    Oct 28, 2019 at 16:26

'"clu_number" = '5'' is not a valid Python string. You are ending the string by the second single quote ('). You can rewrite your clause in different ways so it includes everything. Depending on the type of clu_number you could try the following options:

For a numeric field:

where_clause = "clu_number = 5"

or for a text field:

where_clause = "clu_number = '5'"

Note that the field name does not have to be wrapped with quotes.

  • The field name may or may not need to be wrapped in quotes. Use AddFieldDelimiters() to add any required delimiters.
    – SMiller
    Oct 28, 2019 at 1:26
  • Slight error in the second where_clause, as they're now two separate strings with no concatenation. You could surround it with triple quotes and/or use string formatting.
    – SMiller
    Oct 28, 2019 at 2:14

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.