I am creating a class in which I want to generate a temporary workspace of folders that will persist for the life of the object and then be removed. I am using tempfile.mkdtemp()
in the def __init__
to create the space, but I have read that I can't rely on __del__
being called.
I am wanting something like this:
class MyClass:
def __init__(self):
self.tempfolder = tempfile.mkdtemp()
def ... #other stuff
def __del__(self):
if os.path.exists(self.tempfolder): shutil.rmtree(self.tempfolder)
Is there another/better way to handle this clean up? I was reading about with
, but it appears to only be helpful within a function.