1

I am trying to remove rows from a feature class that is included in a python tool in ArcGIS 10.7. I have run the code as an independent Python script and it runs fine, but when I run the following code, I get an error. tmpfile is a copy of an existing feature that is in a geodatabase.

tmpfile = os.path.basename(tempfile.TemporaryFile().name) 
arcpy.CopyFeatures_management(infile, tmpfile)

### remove unnecessary values
vals = ["Unknown",""]
for val in vals:
    query = """ "DATEDISC" = '%s' """ %val
    tmpfile = arcpy.SelectLayerByAttribute_management(tmpfile,"NEW_SELECTION", query)
    arcpy.DeleteRows_management(tmpfile)

In the standalone script, this removes any rows with the values of "Unknown" or "". In the tool, I receive the error message for arcpy.SelectLayerByAttribute_management() that

The value cannot be a feature class
ERROR 000840: The value is not a Raster Layer.
ERROR 000840: The value is not a Mosaic Layer.
Failed to execute (SelectLayerByAttribute).

This seems like odd behavior for this function.

Does anyone know why this would be happening?

2
  • 1
    We need to know what tmpfile is. How are you setting tmpfile or passing it into your tool?
    – KHibma
    Commented Jul 9, 2020 at 18:45
  • I edited the question. tmpfile is a feature class.
    – user44796
    Commented Jul 9, 2020 at 18:50

1 Answer 1

4

There are several problems with your code. You create a variable called tmpfile and then you overwrite it so destroying the original value. Your variable is a feature class. If you read the help file for the tool select by attribute you will see it takes as input a feature layer not a feature class. You need to convert your feature class into a layer first and feed that into the select tool.

1
  • 1
    You are correct. Also, you can't save the results into a new object. This is allowable in arcpy, but not in the toolbox. So tmpfile = arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION", query) needs to be replaced with arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION", query), where lyr is a layer file created from tmpfile
    – user44796
    Commented Jul 9, 2020 at 19:51

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.