[issue25024] Allow passing "delete=False" to TemporaryDirectory

Nils Kattenbeck report at bugs.python.org
Wed May 26 17:08:52 EDT 2021


Nils Kattenbeck <nilskemail at gmail.com> added the comment:

While the proposal linked by C.A.M. solves one of the use cases but it does not address the others.

One use cases which is rather common for me it is e.g. to have scripts/programs which allow configuring whether temporary directories should get deleted or stay persistent after program exit.
Currently this always requires hand rolled wrappers like the following:

def mkdtemp_persistent(*args, persistent=True, **kwargs):
    if persistent:
        @contextmanager
        def normal_mkdtemp():
            yield tempfile.mkdtemp()
        return normal_mkdtemp(*args, **kwargs)
    else:
        return tempfile.TemporaryDirectory(*args, **kwargs)

with mkdtemp_persistent(persistent=False) as dir:
    ...

Which gets the job done but is not as user friendly as other parts of the stdlib.

----------
nosy: +Nils Kattenbeck

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue25024>
_______________________________________


More information about the Python-bugs-list mailing list