I've written a script in Python that takes in two ascii files that represent surfaces, then convert them to Rasters, and finally Negate their values (i.e. multiply all cells by -1). In the script, I create a temporary folder to store intermediate data and I'd like to delete it after the script has ran, however arcpy.Delete_management does not work. My code is as follows:
import os, arcpy
from arcpy.sa import *
inputInterp = arcpy.GetParameterAsText(0)
inputDiff = arcpy.GetParameterAsText(1)
outputDir = arcpy.GetParameterAsText(2)
#Ascii to Raster Conversion, store output in temp location
arcpy.env.overwriteOutput = True
arcpy.env.workspace = outputDir
tempFolder = outputDir + os.path.sep + "pyOut"
arcpy.CreateFolder_management(arcpy.env.workspace, "pyOut")
outInterp = tempFolder + os.path.sep + "Interp_Rast"
outDiff = tempFolder + os.path.sep + "Diff_Rast"
dtype = "FLOAT"
arcpy.ASCIIToRaster_conversion(inputInterp, outInterp, dtype)
arcpy.ASCIIToRaster_conversion(inputDiff, outDiff, dtype)
#Reversing the rasters to their original values
outIntNegate = Negate(outInterp)
outDiffNegate = Negate(outDiff)
#Cleanup
arcpy.Delete_management(tempFolder)
Now here's the part I do not understand. If I add:
arcpy.Delete_management(outIntNegate)
arcpy.Delete_management(outDiffNegate)
Right before:
arcpy.Delete_management(tempFolder)
Then tempFolder gets deleted as expected. I plan on expanding this script after I get this part working and I really do not want to delete every individual temporary variable I create. I suspect this has something to do with the workspace but I couldn't find any solutions after browsing similar posts. Any ideas?
Answer
I think this is an issue with ArcGIS having open file handles on the files in your temp folder.
As discovered in our comments, you could delete the Python variables and then use the Delete tool to remove the temporary directory (but not the del
statement, that just deletes the variable), but you would still have to explicitly delete each result object...
To get around needing to explicitly delete everything, you can put it all of it into a method and then call that method. When the method returns, anything that is in it's scope is deleted automatically.
Note: this example isn't really ideal since it's passing variables around in the global scope instead of using arguments/return values, but it's very simple.
import os, arcpy
from arcpy.sa import *
inputInterp = arcpy.GetParameterAsText(0)
inputDiff = arcpy.GetParameterAsText(1)
outputDir = arcpy.GetParameterAsText(2)
#Ascii to Raster Conversion, store output in temp location
arcpy.env.overwriteOutput = True
arcpy.env.workspace = outputDir
tempFolder = outputDir + os.path.sep + "pyOut"
arcpy.CreateFolder_management(arcpy.env.workspace, "pyOut")
# Define a method so that the results are in an inner scope
def raster_operations():
outInterp = tempFolder + os.path.sep + "Interp_Rast"
outDiff = tempFolder + os.path.sep + "Diff_Rast"
dtype = "FLOAT"
arcpy.ASCIIToRaster_conversion(inputInterp, outInterp, dtype)
arcpy.ASCIIToRaster_conversion(inputDiff, outDiff, dtype)
#Reversing the rasters to their original values
outIntNegate = Negate(outInterp)
outDiffNegate = Negate(outDiff)
# Call the method defined above
raster_operations()
#Cleanup
arcpy.Delete_management(tempFolder)
No comments:
Post a Comment