3

I'm working with rasters, there are 3 folder (input, query, result). In the input folder are the basic rasters. The user can choose between these rasters, the code can collect the rasters into the query folder, and after it will make a mean raster into the result folder. But after the second run i need to clear the query folder, because it stores the first run rasters and it gives a wrong result. How can i delete these rasters from the query folder, after every run?

 import os, sys
import shutil
import arcpy
from arcpy import env 
arcpy.CheckOutExtension("spatial") 
from arcpy.sa import * 
arcpy.env.overwriteOutput = True 
start_date = arcpy.GetParameterAsText(0)
end_date = arcpy.GetParameterAsText(1) 
env.workspace = "c:/temperature/input" 
rasters = arcpy.ListRasters("*","GRID") 
for raster in rasters: 
    if int(raster) >= int(start_date) and int(raster) <= int(end_date):
        arcpy.CopyRaster_management((raster), "c:/temperature/query/" + (raster) + ".tif", "", "", "", "NONE", "NONE", "", "NONE", "NONE")
env.workspace = "c:/temperature/query" 
eredmeny = "c:/temperature/result/mean.tif" 
rasterses = arcpy.ListRasters("*","TIF") 
result = arcpy.gp.CellStatistics_sa((rasterses), result, "MEAN", "DATA") 
exit()
  • I think you may mean 'But after the first run...', not second. Also not clear on what 'eredmeny' is for, since you don't use it after setting it. But I'm not an SA expert. Anyway, if you're just looking to delete files, there are various python ways to do that, os.remove() being one of the simplest. – recurvata Jan 2 '15 at 20:41
4

you can list the rasters using ListRasters(), then use the arcpy.Delete tool in a loop

for oldraster in rasterses:
    arcpy.Delete_management(oldraster)
| improve this answer | |
  • IndentationError: expected an indented block (create_mean_cc_01_02.py, line 22) Failed to execute (Script). I have this error message, what is the soluiton, have you any idea? – mean1432 Jan 2 '15 at 21:02
  • you need to be consistent with indentation. I used four spaces, so if you copied my line it could be a different indentation than yours (maybe tabs). – radouxju Jan 2 '15 at 21:04

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.