I have a feature class. I want to copy the FC and use a WHERE clause to limit the rows copied. I tried using the CopyFeatures_management but it throws an error on the CopyFeatures_management() line.

Here's the code:

import arcpy

arcpy.env.workspace = 'C:/.../localFile.gdb'

inFC = 'inputFeatureClassName'
outFC = 'tmp_JUNK'

arcpy.CopyFeatures_management(inFC, outFC, '"FIELD_NAME" = "VALUE"')

The error thrown:

arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
Failed to execute (CopyFeatures).

Any ideas what I'm doing wrong and how to fix it?

Update #1 - This code works but I question whether it's really the best approach. If I understand this, I create a feature layer, perform the selection on the feature layer, and then write the selection to a new feature class. Is that correct? Is this the most efficient approach?

    # Import system modules
    import arcpy
    from arcpy import env

    # Set overwrite option
    arcpy.env.overwriteOutput = True

    # Put in error trapping in case an error occurs when running tool

       # Make a layer from the feature class

       arcpy.SelectLayerByAttribute_management("tmpOMG", "NEW_SELECTION", """"FIELD_NAME" = 'VALUE'""")

       # Write the selected features to a new featureclass
       arcpy.CopyFeatures_management("tmpOMG", "C:/.../localFile.gdb/tmpOutFromLayer")

   print arcpy.GetMessages()

source: ArcGIS Help


You can't use a where_clause with Copy Features. Looking at the help page, the syntax is (with optional parameters in {} brackets):

CopyFeatures_management (in_features, out_feature_class, {config_keyword}, {spatial_grid_1}, {spatial_grid_2}, {spatial_grid_3})

So you're passing '"FIELD_NAME" = "VALUE"' as a config_keyword, which is confusing it and causing the error. You can only limit the features copied by starting off with a selection, and selections can only happen on layers (as in your second snippet).

I generally use Feature Class to Feature Class (arcpy.FeatureClassToFeatureClass_conversion), which can accept a where_clause.

FeatureClassToFeatureClass_conversion (in_features, out_path, out_name, {where_clause}, {field_mapping}, {config_keyword})

It's slightly annoying since you have to specify both output workspace and output feature class name, and I don't know why that's the case, but there you go. It's faster than Select_analysis (which does the same operation). For your case, the code would be something like:

arcpy.FeatureClassToFeatureClass_conversion(inFC, outGDB, outFC, '"FIELD_NAME" = "VALUE"')
  • I'm surprised that you say "faster than Select_analysis" - have you any timings to confirm that? – PolyGeo Feb 16 '15 at 20:11
  • 2
    There was one particular subset-copy I needed to do on some very large (~500k features) dataset. FeatureClassToFeatureClass_conversion took 195.85 seconds, Select_analysis took 55738.53 seconds (15.5 hours). I haven't done rigorous testing, although it would be interesting and informative to do so someday; this is extremely anecdotal on my part :) – Erica Feb 16 '15 at 20:14
  • What were the input and output formats? – PolyGeo Feb 16 '15 at 20:17
  • 1
    FWIW, 4 seconds using select_analysis and 10 seconds using FeatureClassToFeatureClass_conversion. Starting with 20k records, filtering down to 1500. I like the "one line of code" solution but now I need to deal with those field mappings. – DenaliHardtail Feb 16 '15 at 20:18
  • 1
    Field mapping is optional for some tools, including FeatureClassToFeatureClass. Since in this case you are making a new feature class, it won't cause an error to leave out field mapping (it'll just copy all the attributes). – Erica Feb 16 '15 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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