I'm attempting to select a certain point in a feature layer so I can perform buffer analysis on it. The code seems fine, but I am getting an invalid expression error:

File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 7221, in SelectLayerByAttribute raise e ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).

Here is my code:

import arcpy
where = """ Location = 'C' """
print where
arcpy.MakeFeatureLayer_management("Q:\Admin\F2F_Scripts\Tools\Template\Intermediate\DATA\Temp.gdb\PADPOINTS", "POINTS")
arcpy.SelectLayerByAttribute_management ("POINTS","NEW_SELECTION", where)
arcpy.Buffer_analysis("POINTS", "Q:\Admin\F2F_Scripts\Tools\Template\Intermediate\DATA\Temp.gdb\Buff", "100 Feet")
del where
  • Try copy/pasting your expression into a select by attribute or a definition query in ArcMap and see if it works for you. – Emil Brundage Mar 7 '16 at 18:34
  • I've already tried and it worked fine. I'm unsure why this error is occurring. – MikPM Mar 7 '16 at 18:37

I suggest looking at ESRI's page http://pro.arcgis.com/en/pro-app/arcpy/get-started/specifying-a-query.htm (Written for Pro, but most of the tools and syntax listed work in Desktop, especially the Add Field Delimiters function.) I hate using SQL queries in ArcPy and avoid it wherever I can, but from the look of their sample code, your query should read closer to "Location" = 'C'.

Check also how your field name is formatted. Is "Location" your field name or your field alias? Go into your feature class's Attribute Table and check that field's properties to figure out whether it's a name or an alias. Given the presence of lowercase letters, I bet that it's an alias, and your field name is different.

  • 1
    You're right, the name of the feature class is different. my script is working now. Thanks for your assistance. – MikPM Mar 7 '16 at 18:48

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.