Is there a way to register a file so that it is deleted when Python exits, regardless of how it exits? I am using long-lived temporary files and want to ensure they are cleaned up.
The file must have a filename and it's original handle should be closed as soon as possible -- there will be thousands of these created and I need to ensure they exist only as normal files.
To delete or rename a file, you must have either delete permission on the file, or delete child permission in the parent directory. To recursively delete the files in a directory, use the SHFileOperation function. To remove an empty directory, use the RemoveDirectory function.
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.
You might be able to use the atexit library but this will not catch functions registered via this module are not called when the program is killed by a signal not handled by Python, when a Python fatal internal error is detected, or when os._exit() is called. But I don't think anything in pure python can handle those cases.
You might also be interested in this question
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With