I have a script that works fine, but now I'd like to remove all the intermediate folders I created therein in order to save memory and to be clean. I've been trying using the "shutil.rmtree ()" function, as I've always done before, but it seems not working this time. Why? Here is my script (the function is at the very end):
import arcpy, os, sys, shutil
try:
newpath_01= r'C:\\Users\\unimi\\Documents\\Umberto\\Universita\\PhD\\Karakoram\\MODIS_Pakistan\\Aspect\\temporary_01'
if not os.path.exists (newpath_01):
os.makedirs (newpath_01)
arcpy.env.workspace = 'C:\\Users\\unimi\\Documents\\Umberto\\Universita\\PhD\\Karakoram\\MODIS_Pakistan\\MODIS_Clip\\Sample_test'
arcpy.env.overwriteOutput= True
from arcpy.sa import *
listMODIS = arcpy.ListRasters ()
arcpy.CheckOutExtension ('Spatial')
for MODIS in listMODIS:
outSnowmask = ExtractByAttributes(MODIS, "VALUE = 200")
outSnowmask.save (newpath_01+ '//'+ 'snow_'+ MODIS)
newpath_02= r'C:\\Users\\unimi\\Documents\\Umberto\\Universita\\PhD\\Karakoram\\MODIS_Pakistan\\Aspect\\temporary_02'
if not os.path.exists (newpath_02):
os.makedirs (newpath_02)
arcpy.ClearEnvironment ('workspace')
arcpy.env.workspace = newpath_01
listMask = arcpy.ListRasters ()
aspect_reclass= 'C:\\Users\\unimi\\Documents\\Umberto\\Universita\\PhD\\Karakoram\\DEM\\per_MODIS\\aspect\\asp_rcls_cknp'
for mask in listMask:
arcpy.CheckOutExtension ('Spatial')
outExtractByMask = ExtractByMask (aspect_reclass, mask)
outExtractByMask.save (newpath_02+ '\\'+ mask)
newpath_03= r'C:\\Users\\unimi\\Documents\\Umberto\\Universita\\PhD\\Karakoram\\MODIS_Pakistan\\Aspect\\temporary_03'
if not os.path.exists (newpath_03):
os.makedirs (newpath_03)
arcpy.ClearEnvironment ('workspace')
arcpy.env.workspace = newpath_02
arcpy.env.overwriteOutput= True
listExtraction= arcpy.ListRasters ()
elev_range= 'C:\\Users\\unimi\\Documents\\Umberto\\Universita\\PhD\\Karakoram\\DEM\\per_MODIS\\recls_CKNPdem.lyr'
for extraction in listExtraction:
arcpy.CheckOutExtension ('Spatial')
name_MODIS= str (extraction [18:25])
ZonalHistogram (extraction, 'ASPECT', elev_range, newpath_03+ '\\'+ name_MODIS+ '.dbf')
arcpy.ClearEnvironment ('workspace')
arcpy.env.workspace = newpath_03
listTable = arcpy.ListTables ()
print listTable
out_dir= 'C:\\Users\\unimi\\Documents\\Umberto\\Universita\\PhD\\Karakoram\\MODIS_Pakistan\\Aspect\\test_CKNP_aspect\\'
for table in listTable:
arcpy.AddField_management (table, 'FILENAME', 'TEXT')
arcpy.CalculateField_management (table, 'FILENAME', '"'+ table+ '"')
arcpy.Merge_management (listTable, out_dir+ 'Snow_cknp_sample.dbf')
shutil.rmtree (newpath_01, newpath_02, newpath_03)
except:
print arcpy.GetMessages ()
Any help will be very appreciated.