Skip to main content
Donate to the Python Software Foundation or Purchase a PyCharm License to Benefit the PSF! Donate Now

Pythonic parameterized cache paths.

Project description Documentation Status

A small package for pythonic parameterized cache paths.

Getting Started

Install: pip install cachepath

Import: from cachepath import CachePath, TempPath, Path

Docs: ReadTheDocs | API doc is here

  1. Integrates pathlib with tempfile.gettempdir and shutil.rmtree by providing TempPath and Path.rm():

    path = TempPath()
    # or would you rather..
    path = None
    with tempfile.NamedTemporaryFile(delete=False) as f:
        path = Path(
    # Only now can we use Path. If we tried using it within the With
    # (for example for path.read_text()), we'd break on Windows
    path.unlink()  # only if file, doesn't work on folders
  2. Wraps pathlib import for Py2/3 compat. (not in six!):

    from cachepath import Path
    # or
    try: from pathlib import Path; except ImportError: from pathlib2 import Path
  3. Provides CachePath, which lets you quickly get a parameterized temp filename, with all folders automatically created:

    r = CachePath(date, userid, 'expensive_results.txt')
    assert (r == Path('/tmp/', date, userid, 'expensive_results.txt')
            and r.parent.exists())
    r.rm()  # File remove
    r.parent.rm()  # Symmetric with folder remove!
    # Without cachepath
    p = Path(tempfile.gettempdir(), date, userid, 'expensive_results.txt').
    # Don't update timestamp if it already exists so that we don't cause
    # Make-like tools to always think something's changed
    if not p.parent.exists():
        p.parent.mkdir(parents=True, exist_ok=True)
    p.unlink()  # Why is it .unlink() instead of .remove()?
    # Why .remove and .unlink, but mkdir instead of createdir?
    # .remove() might throw because there was another file in the folder,
    # but we didn't care, they're tempfiles!
    import shutil

Why, but longer:

Do you need a temp path to pass to some random tool for its logfile? Behold, a gaping hole in pathlib:

import tempfile
import os
try: from pathlib import Path; except ImportError: from pathlib2 import Path
def get_tempfile():
    fd, loc = tempfile.mkstemp()
    os.close(fd)  # If we forgot do this, it would stay open until process exit
    return Path(loc)

# Easier way
from cachepath import TempPath
def get_tempfile():
    return TempPath()  # Path('/tmp/213kjdsrandom')

But this module is called cachepath, not temppath, what gives?

Suppose I’m running that same imaginary tool pretty often, but I’d like to skip running it if I already have results for a certain day. Just sticking some identifying info into a filename should be good enough. Something like Path('/tmp/20181204_toolresults.txt')

# try: from pathlib import Path; except ImportError: from pathlib2 import Path
# We'll cheat a little to get py2/3 compat without so much ugliness
from cachepath import Path
import tempfile
def get_tempfile(date):
    filename = '{}_toolresults.txt'.format(date)
    return Path(tempfile.gettempdir(), filename)

# Easier to do this...
from cachepath import CachePath
def get_tempfile(date):
    return CachePath(date, suffix='.txt')

Not bad, but not great. But our requirements changed, let’s go a step further.

Now I’m running this tool a lot, over a tree of data that looks like this:


I want my logs to be structured the same way. How hard can it be?


Let’s find out:

# Let's get the easy way out of the way first :)
def get_path(date, person):
    return CachePath(date, person, suffix='_output.txt')
    # Automatically ensures /tmp/date/ exists when we create the CachePath!

# Now the hard way
def get_path(date, person):
    personfilename = '{p}_output.txt'.format(p=person)
    returning = Path(tempfile.gettempdir())/date/personfilename
    # Does this mkdir update the modified timestamp of the folders we're in?
    # Might matter if we're part of a larger toolset...
    returning.parent.mkdir(exist_ok=True, parents=True)
    return returning

Suppose we hadn’t remembered to make the $date/ folders. When we passed the Path out to another tool, or tried to .open it, we may have gotten a Permission Denied error on Unix systems rather than the “File/Folder not found” you might expect. With CachePath, this can’t happen. Creating a CachePath implicitly creates all of the preceding directories necessary for your file to exist.

Now, suppose we found a bug in this external tool we were using and we’re going to re-run it for a day. How do we clear out that day’s results so that we can be sure we’re looking at fresh output from the tool? Well, with CachePath, it’s just:

def easy_clear_date(date):
    CachePath(date).clear()  # rm -r /tmp/date/*

But if you don’t have cachepath, you’ll find that most Python libs play it pretty safe when it comes to files. Path.remove() requires the folder to be empty, and doesn’t provide a way to empty the folder. Not to mention, what if our results folders had special permissions, or was actually a symlink, and we had write access but not delete? Oh well, let’s see what we can do:

def hard_clear_date(date):
    # We happen to know that date is a folder and not a file (at least in our
    # current design), so we know we need some form of .remove() rather than
    # .unlink(). Unfortunately, pathlib doesn't offer one for folders with
    # files still in them. If you google how to do it, you will find plenty of
    # answers, one of which is a pure pathlib recursive solution! But we're lazy,
    # so lets bring in yet another module:
    p = Path(tempfile.gettempdir(), date)
    import shutil
    if p.exists():
    p.mkdir(exist_ok=True, parents=True)
    # This still isn't exactly equivalent to CachePath.clear(), because we've
    # lost whatever permissions were set on the date folder, and if it were
    # actually a symlink to somewhere else, that's gone now.

Convinced yet? pip install cachepath or copy the source into your local (you know you have one.)

API doc is here.

By the way, as a side effect of importing cachepath, all Paths get the ability to do rm() and clear().

Shameless Promo

Find yourself working with paths a lot in cmd-line tools? You might like invoke and/or magicinvoke!

[*]The source for CachePath can be downloaded from the Github repo.
[†]This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.


1.0.0 (2018-12-08)

  • Big doc updates. 1.0.0 to symbolize SemVer adherence.

0.1.0 (2018-12-08)

  • First release on PyPI. Adds CachePath, TempPath, Path.

Project details

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
cachepath-1.1.1-py2.py3-none-any.whl (8.0 kB) Copy SHA256 hash SHA256 Wheel py2.py3
cachepath-1.1.1.tar.gz (13.5 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page