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

    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)

    print arcpy.GetMessages ()

Any help will be very appreciated.

  • Is it giving an error? Silently failing? Only deleting some files? – Jason Scheirer Sep 27 '13 at 15:02

The shutil.rmtree function takes only one path argument. See help(shutil.rmtree):

rmtree(path, ignore_errors=False, onerror=None)

You've given it three and so the second and third are bound to ignore_errors and oneerror variables, causing (I assume) TypeErrors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.