pythonpython-2.7

How to remove file when program exits?


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.


Solution

  • 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.