1

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?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Aug 4, 2015 at 23:05
5
  • So are you saying that "arcpy.Delete_management(outIntNegate)" and "arcpy.Delete_management(outDiffNegate)" are the bits not getting deleted? Commented Aug 5, 2015 at 1:22
  • 1
    Have you tried something like this: stackoverflow.com/questions/303200/… Commented Aug 5, 2015 at 1:46
  • What happens when you delete the result variables (del outIntNegate) instead of using the Delete tool for each one? I suspect this is happening because ArcGIS is still referencing the files within the folder (which you are freeing when you use the delete tool). Commented Aug 5, 2015 at 11:17
  • @TsvGis I'm saying that tempFolder doesn't get deleted, however it is dependent on weather or not outIntNegate and outDiffNegate are deleted properly. Commented Aug 5, 2015 at 15:06
  • @EvilGenius, If I do that tempFolder Deletes properly! However, I found that if I do: del outIntNegate del outDiffNegate del tempFolder rather than: del outIntNegate del outDiffNegate arcpy.Delete_management(tempFolder) tempFolder does not get deleted. Commented Aug 5, 2015 at 15:29

1 Answer 1

4

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)
answered Aug 5, 2015 at 16:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.