def tempfilename(**kwargs):
    """
    Reserve a temporary file for future use.

    This is useful if you want to get a temporary file name, write to it in the
    future and ensure that if an exception is thrown the temporary file is removed.
    """
    kwargs.update(delete=False)
    try:
        f = NamedTemporaryFile(**kwargs)
        f.close()
        yield f.name
    except Exception:
        if os.path.exists(f.name):
            # Ensure we clean up after ourself
            os.unlink(f.name)
        raise
Reserve a temporary file for future use.

    This is useful if you want to get a temporary file name, write to it in the
    future and ensure that if an exception is thrown the temporary file is removed.