Hello. I have just starting using Arcpy on Python2.7 and I am saving raster datasets as tiff files using .save() on raster objects as explained here (http://pro.arcgis.com/en/pro-app/arcpy/classes/raster-object.htm). I am saving enough that I am encountering memory issues half way through my code, causing it to crash. I believe python is not closing these files after it saves them. I can use psutil to view the files which are currently opened by python and I see that they are all still open, and windows also tells me they are still 'open in pythonw' if I try to delete them. I have tried assigning the command itself to a variable and deleting this, but it does not close the file (even after an explicit garbage collection call), nor does it close the file if you delete the raster variable itself.
How do you close a raster which has been created/opened by the .save() command without closing python?
I'm having this problem too. Here is a piece of example code representative of what I'm trying to do.
from arcpy import sa raster_elevation = sa.Raster("elevation") raster_slope = sa.Slope(raster_elevation, "DEGREE") raster_slope.save("slope")
When I try to delete or move the files referenced above (after the script is finished) from my file browser, I get the error, "The action can't be completed because the file is open in pythonw.exe". This happens to both raster files. Closing the Python shell completely removes the hold on the files, but I would like to be able to manipulate the files (from outside arcpy) after the script is finished and before I close the shell.
Thanks for your help!
finicky arcpy is... in theory,
del raster_elevation, raster_slope
and for good measure
should remove the reference from python, but there is no guarantee
sometimes what I do, is have two instances of the same script open in two different IDEs and I just kill the one that was running the script and carry on in the other one. A bit of overkill, but separate python instances tend to play nice if arcpy gets too hungry.
And by the way... if arcmap is open, the lock can remain, so I rarely have it open unless I have to see the result. And if I have seen it, I remove the result, save the project and hope all references are killed during the save.
Finicky is a good word. I used the 'del' statements as suggested and placed a 'gc.collect()' at the end as suggested from some other threads to no avail. It turns out that I can modify the output file while the shell is still available while the input files continue to be locked. Also, I ensured that ArcMap is not running. I'll keep my eyes peeled for a simple solution. Thanks for your time, Dan.
Hi Dan and Tyler,
Apologies for not getting back to you sooner, it's been a hectic few weeks starting this new job and trying to work out how arcpy and everything works. I did try to make some example code to send you but this wasn't suffering the same problems and deleting the variables was cutting the connection with the files and freeing the memory as hoped. And now my code is working using this 'del' method and I couldn't work out what had changed to share with you. I can only think that you are right about it not working when Arcmap is open because it would have been open when I first suffered this problem and asked the question as I'd just installed it to get the code working in the first place, whereas I haven't opened it since and everything works now. But since Tyler is having the same problem I first had then there is perhaps more to it and something more complicated afoot.
Obviously it isn't ideal to delete the variables and even arcpy itself, especially when arcpy is so finicky about whether this will actually work; so please let us know if you work out a better solution.
Cheers for the help!