Select to view content in your preferred language

Delete temporary lyr after code interrupted

675
4
06-03-2011 01:16 AM
chriss_
Emerging Contributor
Hi!
When programming I often use temporary Layers. E.g. "Make Feature Layer (Data Management)". Normally I delete those files at the end of my loop. But when i am still writing the code and test it, python often interrupts because it discovers whatever mistake. My problem is that now I have all this temporary layers that are blocked?/not deleted?. So next time i rund my code it states that the temporary layer does already exist. Is there any way to delete all this temp layers when the code is interrupted? or a way to creat unique layers/files/whatever everytime i run the code?

thanks for your help
chris
Tags (2)
0 Kudos
4 Replies
RDHarles
Regular Contributor
You could use a try/except like this:

try:
    arcpy.MakeFeatureLayer_management(dir+"/cty"+dir+"xxx.shp", "clplyr")
    arcpy.SelectLayerByAttribute_management("clplyr", "New_Selection", "ID = "+str(id)+"")
except:
    print "\nError!\n"
    arcpy.Delete_management("clplyr")
0 Kudos
KimOllivier
Honored Contributor
Deleting layers never worked for me, maybe it does now in 10.
My solution is to set gp.overwriteoutput = True or arcpy.env.overwriteOutput = True
Then you can always overwrite an in-memory layer definition.
0 Kudos
chriss_
Emerging Contributor
Hi!
Thanks for your answer. I also had problems to delete the temp. shapefiles. So i think i ll try what oliver suggests.

Thank you
0 Kudos
AndresSevtsuk
Emerging Contributor
Has there been an answer to this? Anyone found a way to have arcpy scripts still do cleanup after a user interrupts by pressing "cancel" on the GUI?

Thanks,

Andres
0 Kudos