Skip to main content

TempDirectoryContext

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.

Metadata

Release files for TempDirectoryContext 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for TempDirectoryContext 1.0.1
File Interpreter ABI Platform Actions
TempDirectoryContext-1.0.1-py2.py3-none-any.whl Python 2, Python 3 none any Details for TempDirectoryContext-1.0.1-py2.py3-none-any.whl

Release files / TempDirectoryContext-1.0.1-py2.py3-none-any.whl

Download URL TempDirectoryContext-1.0.1-py2.py3-none-any.whl
Size 7.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
e9ce32ce29cada0c87b76b488da883bd89e2fdab29876182b985838cb8261f82
BLAKE2b-256 checksum
How to use checksums
43c9b3ab4dbdd6f907c46f086462dd87afa39b6219aab76055dddb44220a68ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for TempDirectoryContext 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page