Select to view content in your preferred language

deleting used files

898
2
03-30-2014 06:38 PM
benberman
Regular Contributor
The code works, I just want to be able to delete the intersected output.shp and the frequency table once the code finishes looping through the featureclasses. I tried doing a Delete_management but it terminated the code after printing the first feature class. Any ideas how to dynamically execute the file cleanup?

 
                      arcpy.env.workspace= workspace
                        arcpy.env.OverwriteOutput = True
                        lstfc = sorted(arcpy.ListFeatureClasses())
                        for fc in lstfc:
                            arcpy.Intersect_analysis([fc,intersected feature class],path to intersected output.shp)
                            arcpy.MakeFeatureLayer_management(path to intersected output.shp,"fclayer","PERSON='' OR PERSON IS NULL")
                            arcpy.Frequency_analysis("fclayer",path to frequency table,["PERSON_1"])
                            lststate=set([row.getValue("PERSON_1") for row in arcpy.SearchCursor(path to frequency table)])
                            newlist=sorted(lststate)
                            for state in newlist:
                                print state + " " + fc
Tags (2)
0 Kudos
2 Replies
markdenil
Frequent Contributor
I like to both delete any leftover temp files before creating the current one
and cleaning up afterwards.
Belt and suspenders, so to speak....
This code snip is bits taken from a much longer routine
with lots of intermediate files.
import arcpy

def whacka(mole):
    if finda(mole):
        arcpy.Delete_management(mole)

def finda(thing):
    if arcpy.Exists(thing):
        return True
    else:
        return False

tempList = []

''' bunch of other stuff,
including other temp files added to tempList '''

finalLayer = r"%s" % (layerName)
finalLayerFile = r"%s\%s.lyr" % (work, finalLayer)
tempList.append(finalLayerFile)           
whacka(finalLayerFile)

arcpy.MakeFeatureLayer_management(
                            final,
                            finalLayer)
arcpy.ApplySymbologyFromLayer_management(
                            finalLayer,
                            symFile)
arcpy.SaveToLayerFile_management(
                            finalLayer,
                            finalLayerFile)

''' more stuff,
including exporting the finalLayerFile to kmz
and then blowing the finalLayerFile away
before looping through the whole thing again'''


if verbose: print '...housekeeping work'
for temp in tempList:
    whacka(temp)

0 Kudos
benberman
Regular Contributor
@mdenil, I appreciate the detailed the response. However, I only need some simple direction as far as where to place the "clean up tools" relative to the snippet I provided. Perhaps, just a quick code with my snippet included would do....thanks.
0 Kudos