raster created by arcpy.sa.FocalStatistics cannot be deleted

373
3
Jump to solution
07-23-2013 10:58 AM
PKennedy
New Contributor
I've successfully created a raster using FocalStatisitics:
  smoothed = arcpy.sa.FocalStatistics('input', neighborhood, "MEAN","NODATA")   smoothed.save('output.tif')


Problem:
The problem is that when I try to delete the 'output.tif' file within Python, I get the following error:

WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'E:/output.tif'


The problem also occurs in an interactive Python session - the file cannot be deleted by Windows Explorer until the PythonWin is closed.  I assume the problem is that FocalStatistics did not close the file it created.

How can I delete 'output.tif' within Python?  I am new to arcpy so I may be missing something obvious.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
Have you tried deleting your smoothed variable?

View solution in original post

0 Kudos
3 Replies
MathewCoyle
Frequent Contributor
Have you tried deleting your smoothed variable?
0 Kudos
JamesCrandall
MVP Frequent Contributor
I've successfully created a raster using FocalStatisitics:
  smoothed = arcpy.sa.FocalStatistics('input', neighborhood, "MEAN","NODATA")
  smoothed.save('output.tif')


Problem:
The problem is that when I try to delete the 'output.tif' file within Python, I get the following error:



The problem also occurs in an interactive Python session - the file cannot be deleted by Windows Explorer until the PythonWin is closed.  I assume the problem is that FocalStatistics did not close the file it created.

How can I delete 'output.tif' within Python?  I am new to arcpy so I may be missing something obvious.


Or I wonder if a with statement would be appropriate?  (not sure if this would work, it is NOT tested), but it should handle the termination for you (from what I've read anyway):


  with arcpy.sa.FocalStatistics('input', neighborhood, "MEAN","NODATA") as smoothed:
      smoothed.save('output.tif')



(someone please correct this if it is wrong.  Sorry OP for not testing this, just a quick thought that might work as expected).
0 Kudos
PKennedy
New Contributor
Have you tried deleting your smoothed variable?


Thanks! Delete_management is the answer.

What works is the following code:
arcpy.Delete_management('output.tif')


My mistake was that I tried deleting smoothed using Delete_management (but obviously that didn't work).

Thanks again.
0 Kudos