Use the tempfile
module; it creates temporary files that auto-delete.
From the tempfile.NamedTemporaryFile()
documentation:
If delete is true (the default), the file is deleted as soon as it is closed.
You can use such a file object as a context manager to have it closed automatically when the code block exits, or you leave it to be closed when the interpreter exits.
The alternative is to create a dedicated temporary directory, with tempdir.mkdtemp()
, and use shutil.rmtree()
to delete the whole directory when your program completes.
Preferably, you do the latter with another context manager:
import shutil
import sys
import tempfile
from contextlib import contextmanager
@contextmanager
def tempdir():
path = tempfile.mkdtemp()
try:
yield path
finally:
try:
shutil.rmtree(path)
except IOError:
sys.stderr.write('Failed to clean up temp dir {}'.format(path))
and use this as:
with tempdir() as base_dir:
# main program storing new files in base_dir
# directory cleaned up here
You could do this with a atexit
hook function, but a context manager is a much cleaner approach.