Skip to main content

Context manager used to maintain your temporary directories/files.

Project description


Build Status codecov Code Climate Issue Count

PyPI Version PyPI Versions

Context manager to maintain your temporary directories/files.


To install scratchdir from pip:

    $ pip install scratchdir

To install scratchdir from source:

    $ git clone
    $ python install


Creating a new ScratchDir is simple. Just instantiate a new instance and call setup:

⇒  cat examples/readme/
import scratchdir

sd = scratchdir.ScratchDir()

⇒  python examples/readme/

Or as a context manager using the with statement:

⇒  cat examples/readme/
import scratchdir

with scratchdir.ScratchDir() as sd:

⇒  python examples/readme/

Files created by the ScratchDir are automatically cleaned up on teardown:

⇒  cat examples/readme/
import os
import scratchdir

path = None

with scratchdir.ScratchDir() as sd:
    tmp = sd.named(delete=False)
    path =
    print('Path {} exists? {}'.format(path, os.path.exists(path)))

print('Path {} exists? {}'.format(path, os.path.exists(path)))

⇒  python examples/readme/
Path /var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/y1aedyk8.scratchdir/tmp7m79rev1 exists? True
Path /var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/y1aedyk8.scratchdir/tmp7m79rev1 exists? False

Directories within the ScratchDir are also easy to create:

⇒  cat examples/readme/
import scratchdir

with scratchdir.ScratchDir() as sd:
    subdir =

⇒  python examples/readme/

Methods on the ScratchDir instance will pass arguments down to their corresponding functions in tempfile.

⇒  cat examples/readme/
import scratchdir

with scratchdir.ScratchDir() as sd:
    tmp = sd.named(suffix='.txt', prefix='logfile-', delete=False)

⇒  python examples/readme/

Creating a hierarchy of ScratchDir instances to match that of your domain objects is also simple:

⇒  cat examples/readme/
import scratchdir

with scratchdir.ScratchDir(prefix='grandparent-') as grandparent:
    with grandparent.child(prefix='parent-') as parent:
        with parent.child(prefix='child-') as child:

⇒  python examples/readme/


The ScratchDir instance maintains a set of bound methods are map to functions/classes within the tempfile module in the standard library. A table of methods is as follows:

scratchdir tempfile description
file, TemporaryFile TemporaryFile Create a nameless temporary file that is automatically deleted once it's closed.
named, NamedTemporaryFile NamedTemporaryFile Create a temporary file that receives a filename on disk that is automatically deleted once it's closed unless the delete parameter is False.
spooled, SpooledTemporaryFile SpooledTemporaryFile Create a temporary file that will overflow from memory onto disk once a defined maximum size is exceeded.
secure, mkstemp mkstemp Create a temporary file in as secure way as possible.
directory, mkdtemp mkdtemp Create a temporary directory.
filename N/A Create a unique filename within the ScratchDir.
join N/A Join a number of paths to the root of the ScratchDir.


I've implemented similar functionality three times now, starting with my scatter project back in 2013-2014. I'd rather not write it again, so the goal is that scratchdir should be generic and reusable for future projects.


If you would like to contribute, simply fork the repository, push your changes and send a pull request.


Scratchdir is avaialble under the Apache 2.0 license.

Project details

Download files

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

Source Distribution

scratchdir-1.0.2.tar.gz (10.3 kB view hashes)

Uploaded source

Built Distribution

scratchdir-1.0.2-py2.py3-none-any.whl (10.2 kB view hashes)

Uploaded py2 py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page