Package: scratchdir
scratchdirContext manager to maintain your temporary directories/files.InstallationTo install scratchdir frompip:$pipinstallscratchdirTo install scratchdir from source:$gitclonegit@github.com:ahawker/scratchdir.git$pythonsetup.pyinstallUsageCreating a newScratchDiris simple. Just instantiate a new instance and callsetup:⇒catexamples/readme/setup.py
importscratchdirsd=scratchdir.ScratchDir()sd.setup()print(sd.wd)sd.teardown()⇒pythonexamples/readme/usage-1.py
/var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/3e56r54m.scratchdirOr as a context manager using thewithstatement:⇒catexamples/readme/context-manager.py
importscratchdir

withscratchdir.ScratchDir()assd:print(sd.wd)⇒pythonexamples/readme/context-manager.py
/var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/_ibfhq1s.scratchdirFiles created by theScratchDirare automatically cleaned up on teardown:⇒  cat examples/readme/cleanup.py
import os
import scratchdir

path = None

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

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

⇒  python examples/readme/cleanup.py
Path /var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/y1aedyk8.scratchdir/tmp7m79rev1 exists? True
Path /var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/y1aedyk8.scratchdir/tmp7m79rev1 exists? FalseDirectories within theScratchDirare also easy to create:⇒catexamples/readme/directory.py
importscratchdir

withscratchdir.ScratchDir()assd:subdir=sd.directory()print(subdir)⇒pythonexamples/readme/directory.py
/var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/c1odkxbw.scratchdir/tmpcyeqjk1vMethods on theScratchDirinstance will pass arguments down to their corresponding functions intempfile.⇒catexamples/readme/params.py
importscratchdir

withscratchdir.ScratchDir()assd:tmp=sd.named(suffix='.txt',prefix='logfile-',delete=False)print(tmp.name)⇒pythonexamples/readme/params.py
/var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/1h_7379t.scratchdir/logfile-z1gq195q.txtCreating a hierarchy ofScratchDirinstances to match that of your domain objects is also simple:⇒catexamples/readme/hierarchy.py
importscratchdir

withscratchdir.ScratchDir(prefix='grandparent-')asgrandparent:print(grandparent.wd)withgrandparent.child(prefix='parent-')asparent:print(parent.wd)withparent.child(prefix='child-')aschild:print(child.wd)⇒pythonexamples/readme/hierarchy.py
/var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/grandparent-4ld_pl8f.scratchdir
/var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/grandparent-4ld_pl8f.scratchdir/parent-s6y_gmxg.scratchdir
/var/folders/86/zhtx1pv53qs2mm1fq1k1841w0000gn/T/grandparent-4ld_pl8f.scratchdir/parent-s6y_gmxg.scratchdir/child-28k2hpdk.scratchdirMethodsTheScratchDirinstance maintains a set of bound methods are map to functions/classes within thetempfilemodule in the standard library. A table of methods is as follows:scratchdirtempfiledescriptionfile, TemporaryFileTemporaryFileCreate a nameless temporary file that is automatically deleted once it's closed.named, NamedTemporaryFileNamedTemporaryFileCreate a temporary file that receives a filename on disk that is automatically deleted once it's closed unless thedeleteparameter isFalse.spooled, SpooledTemporaryFileSpooledTemporaryFileCreate a temporary file that will overflow from memory onto disk once a defined maximum size is exceeded.secure, mkstempmkstempCreate a temporary file in as secure way as possible.directory, mkdtempmkdtempCreate a temporary directory.filenameN/ACreate a unique filename within theScratchDir.joinN/AJoin a number of paths to the root of theScratchDir.GoalsI've implemented similar functionality three times now, starting with myscatterproject back in 2013-2014.
I'd rather not write itagain, so the goal is that scratchdir should be generic and reusable for future projects.ContributingIf you would like to contribute, simply fork the repository, push your changes and send a pull request.LicenseScratchdir is avaialble under theApache 2.0license.