1

I'm trying to write a script that deletes the rasters that have no information at all. Some people told me to use the try/except, using the statistics as a factor to determine if the information exist or not. But when I run the script in my model, it says it's complete, but the image with no information is still there! What am I doing wrong?

This is the script:

import arcpy
from arcpy import env
arcpy.env.workspace = arcpy.GetParameterAsText(0)
rasters = arcpy.ListRasters("*", "TIF")
for raster in rasters:
    try: 
        arcpy.GetRasterProperties_management (raster, "MINIMUM", "")            
    except:
        arcpy.Delete_management(raster,"")
  • Your script works fine for me. Have you tried adding some print statements to determine which rasters it is finding, and which rasters it is trying to delete? – Midavalo Oct 10 '17 at 20:20
  • An empty raster can have calculated statistics (so the GetRasterProperties_management() won't error out). Are you sure your raster has no statistics? – Midavalo Oct 10 '17 at 21:15
  • Yes! It has no statistics, an even when i try to use calculate statistics, it doesn't change anything... – Charlera Oct 11 '17 at 12:00
2

If you want to delete rasters where all cells are NODATA then this logic would work:

import arcpy
from arcpy import env
    arcpy.env.workspace = arcpy.GetParameterAsText(0)
    rasters = arcpy.ListRasters("*", "TIF")
    for raster in rasters:
        resObj = arcpy.GetRasterProperties_management(raster, "ALLNODATA") 
        bAllNoData =resObj.getOutput(0)
        if bAllNoData == 1:
            arcpy.Delete_management(raster)

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.