Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove file when program exits? [closed]

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.

like image 597
edA-qa mort-ora-y Avatar asked Jan 21 '14 12:01

edA-qa mort-ora-y


People also ask

How to delete file in win32?

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.


2 Answers

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.

like image 62
Martijn Pieters Avatar answered Oct 06 '22 08:10

Martijn Pieters


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

like image 39
wdh Avatar answered Oct 06 '22 07:10

wdh