Skip to main content

A context manager around the creation of temporary directories

Project description

The TempDirectoryContext provides a simple to use library that provides a context manager around the creation and deletion of temporary directories.

The module provides a context manager around the tempfile.mkdtemp library calls, and providing automated clean up of the directories. By default the context manager keeps up to 3 historic temporary directories - especially useful for testing and debugging. It is possible to configure the following properties :
  • The Directory name Prefix and Suffix.

  • The number of historic directories to be kept

  • Whether to delete historic directories at all - here be dragons - be careful

As an example

import TempDirectoryContext as TempDirCont

with TempDirCont.TempDirectoryContext() as tmp:
    #
    # A temporary directory is created under the standard tmp directory - probably /tmp
    # The directory will be named : /tmp/tmp<xxxxxx>TempDirCont - where <xxxxxx> is a random 6 character string
    # (Assuming that the standard tmp directory is /tmp
    #
The context manager accept the following arguments:
  • prefix : The first characters of the directory name (defaults to tmp)

  • suffix : The end characters of the directory name (defaults to TempDirCont)

  • root : The directory into which the temporary directory is created (defaults to tempfile.gettempdir() )

  • delete_historic : A Boolean to determine if previous directories are deleted (defaults to True)

  • keep_max : The maximum number of historic directories to keep (defaults to 3)

As indicated the context manager keeps track of historically created temporary directory and attempts to clean them up. The clean up action takes place when the context manager exits. Historic directories are deleted in date order, and are counted based on the suffix and prefix values. The delete historic and keep_max arguments are applied on the basis of the prefix and suffix as well - allowing you to apply different deletion strategies to different sets of temporary directories.

Note : The delete_historic and keep_max arguments are re-applied when the context manager is exited. This could lead to directories being deleted, despite the directory being seemingly protected when it was created: An example :

import TempDirectoryContext as TempDirCont


def funca():
    with TempDirCont.TempDirectoryContext(delete_historic=False) as tmp:
        # Some code
    # Nothing will be deleted when the above context manager exits

def funcb():
    with TempDirCont.TempDirectoryContext(keep_max=1, delete_historic=True) as tmp:
        # Some code
    # All historic temp directories will be deleted when the above context manager exits
    # with the exception of the last directory created (i.e. the one created by the context manager above).


funca()
funcb()
# At this point the directory created in funca() will be deleted
# despite the argument ``delete_historic`` being False when the context manager is invoked.

To avoid this, you should ensure that the delete_historic and keep_max settings are kept consistent when the same prefix and suffix are used, as the deletion process is applied on the basis of the prefix and suffix, rather than across all temporary directories.

Use Case

The main use case for this library is for testing, where you might want to create a sandbox directory (which can be safely deleted), but which is retained for debugging purposes. By using different prefixes and suffixes you can create multiple sets of sandboxed directories with different prefixes and suffixes - so it is clear what files pertain to which tests.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

TempDirectoryContext-1.0.1-py2.py3-none-any.whl (7.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file TempDirectoryContext-1.0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for TempDirectoryContext-1.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e9ce32ce29cada0c87b76b488da883bd89e2fdab29876182b985838cb8261f82
MD5 4e78dd3463d51dca62789f3db14f01dd
BLAKE2b-256 43c9b3ab4dbdd6f907c46f086462dd87afa39b6219aab76055dddb44220a68ef

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page