Clear cache files older than 30 minutes
Instead of this:


for f in os.listdir(cache):
        # ...
        use_by = time.time() - 30 * 60
        if os.path.getatime(f) &lt; use_by:
                # ...



Consider this:

use_by = time.time() - 30 * 60
for f in os.listdir(cache):
        # ...
        if os.path.getatime(f) &lt; use_by:
                # ...


In this version, use_by is set only once, before the loop begins,
which is slightly more efficient.
Unless, you really want the timing to be re-evaluated in every iteration.



It can be error-prone and lead to nasty bugs to re-assign the loop variable (as well as method arguments), like here:


for f in os.listdir(cache):
        f = os.path.join(cache, f)



And in any case, I would recommend using more meaningful variable names, for example:

for filename in os.listdir(basedir):
    path = os.path.join(basedir, filename)




The code you posted sounds like a good idea.
It could be turned into a reusable, general function with parameters:


basedir: base directory to clean
limit: time limit to use, in minutes


Something like this:

def wipe_unused(basedir, limit):
    """
    Remove files in *basedir* not accessed within *limit* minutes

    :param basedir: directory to clean
    :param limit: minutes
    """
    atime_limit = time.time() - limit
    count = 0
    for filename in os.listdir(basedir):
        path = os.path.join(basedir, filename)
        if os.path.getatime(path) &lt; atime_limit:
            os.remove(path)
            count += 1
    print("Removed {} files.".format(count))


if __name__ == '__main__':
    wipe_unused(os.path.join(os.getcwd(), 'cache'), 30 * 60)