Repository navigation
Allow delete=False for tempfile.TemporaryDirectory() #100131
Description
Activity
- addedtype-featureA feature request or enhancementA feature request or enhancement
on Dec 9, 2022 - addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Dec 9, 2022 Just use
mkdtemp().The purpose of the context manager is automatic deletion of the directory when you leave the block. If you do not need this, you do not need a contect manager.
Reacted by Thomas Grainger, Alex Waygood, Erlend E. Aasland, Gregory P. Smith, Saar Tochner, Rik van der Kooij, Georg_R and pangyutengReacted by James Darren Muir and GPlathere's one additional way to archive this:
with ( contextlib.nullcontext(tempfile.mkdtemp()) if args.keep_temp_files else tempfile.TemporaryDirectory() ) as dir: do_something(dir)
Reacted by Allen Lee, Guy Sheffer, Ethan Ho, Callan Gray and David FosterReacted by Guy ShefferJust use mkdtemp().
The purpose of the context manager is automatic deletion of the directory when you leave the block. If you do not need this, you do not need a contect manager.
I know that. The
tempfile.TemporaryDirectory()is used for exactly that purpose. But sometimes, you need to debug a code that uses the context manager. So this Feature Request just adds for a Way to disable the normal behaviour ( using a breakpoint is not always possible/practical) , so you you can do your debugging stuff without doing much code rewrite and after that, you just remove thedelete=False. This is not meant to be used, when you permanently don't want to delete the temp directory.there's one additional way to archive this:
This is not suitable, if you are not a Python expert
Reacted by Jean-Christophe Fillion-Robin, Andy Mroczkowski and James Darren MuirI'm going to close this based on Serhiy's practical
mkdtemp()suggestion above. We've already got one way to do this.Reacted by Alex Waygood and Erlend E. AaslandThe point of
TemporaryDirectoryis to make handling the lifecycle of a temporary directory convenient. There are other ways to achieve the desired result, but it's hard to call any of them convenient.Reacted by C.A.M. Gerlach, Carl Meyer, Rolf Bippus, John Hagen, Kayvon Tabrizi, Eduardo Mucelli, Jules Gagnon-Marchand, James Darren Muir, GPla and Rodolfo FigueroaFWIW, given the basic stated motivation for this feature, both on this issue and the previous one, is cases in which the cleanup may or may not be required, the decent number of users who presented similar use case, it's not clear to me how "just use
mkdtemp" is a good solution for that.Sure, there are workarounds, but the OP illustrates the issues with each, and in summary the obvious ones are kludgy and the non-kludgy ones are not obvious. It also seems like it could increase the chance users reach for the more cryptic lower-level interfaces for the other cases where the higher-level one would be safer, simpler and more ergonomic.
In light of the fact that the code changes to the stdlib are relatively minimal, and there's an up to date PR with tests and docs provided, I'm inclined to agree with @zware that this change shouldn't be rejected on that basis.
Reacted by Zachary Ware, Carl Meyer, Pras Velagapudi, Zachary Foster, yalpotuA, Greg Swift, Andy Mroczkowski, acdupont and James Darren Muirfair enough. I really don't think this feature will see much use, but I've used the PR to improve the docstring anyways.
It still feels quite odd to me to say "We have a
TemporaryDirectoryclass, but it's not always temporary -- you can make it not-temporary by using this special flag we added".There's been a number of different ways of doing this pointed out in the above thread, but FWIW I'd probably use a
contextlib.ExitStackto achieve this:with ExitStack() as stack: if args.keep_temp_files: dir = stack.enter_context(TemporaryDirectory()) else: dir = mkdtemp() do_something(dir)
It still feels quite odd to me to say "We have a TemporaryDirectory class, but it's not always temporary -- you can make it not-temporary by using this special flag we added".
I don't think arguing on the basis of the meaning of "temporary" holds much water here.
mkdtemp()also has "temp" in the name and lives intempfile.py, but that doesn't mean Python deletes it for you. "Temporary" here has a broader meaning: it's created in some distinguished location for throwaway files, where quite possibly some OS-level automated process may come along and clean it up at some point.The ExitStack solution and the nullcontext solution are both just fine, but they are quite a bit more verbose. The use-case that I find most convincing is the "temporary (ahem) debugging" use case, where you have a TemporaryDirectory that you generally do want cleaned up, but in the course of some debugging you need to leave it around to look at. I've definitely had this situation, and reindenting a bunch of code or adding a new import for
nullcontextare not very attractive quick options.Reacted by Alex Waygood, C.A.M. Gerlach, Zachary Foster, yalpotuA, Wim Jeantine-Glenn, Antoni Felczyński, Minhu Wang, acdupont, Eric Cong, James Darren Muir and 3 moreIt may seem odd at first glance, but there are even non-debugging uses - you may have code that needs cleanup to be conditional based on user input. This, perhaps unusual, feature provides an easy way to do that without adopting unusual code structures.
I added the missing documentation to the PR and described this in there as I expect others to look at the feature with the same surprise I initially had as well.
Reacted by acdupont and James Darren Muir- added a commit that references this issue
on Mar 24, 2023 Thanks all, including the contributed PR and the discussion. Merged. You changed my mind. :)
Reacted by Zachary Ware, C.A.M. Gerlach, JakobDev, Gavin Ridley, rudy-6-4, Zachary Foster, yalpotuA, Ben Mares, John B, Jean-Christophe Fillion-Robin and 13 more
Feature or enhancement
If you use
tempfile.TemporaryDirectory(), the directory is automagically deleted, what is the whole purpose of this class. But sometimes you may want to take a look at it for debugging reasons, so it would be nice to have adelete=Falseto prevent deleting.Pitch
Code using this could look like this:
I personal use
tempfile.TemporaryDirectory()a lot, especial in build scripts. Sometimes a need to take a look at the content of these. so having this Option would be great for that.Other ways to achieve this
Of course, there would be other ways to achieve this.
1. Use a extra function that takes the directory as argument:
This would need some code rewrite.
2. Using
input()This could cause problems in situations, where you can't easily give some Keyboard input to TTY.
3. Using a custom function
This will blow up your code when you just write a simple script. Python also often used by beginners, which may don't know, they can do this and start refactoring their code.
Why you should implement this
The argument can be easy added to exiting scripts if needed, without any additional work. A scripting language like Python should allow this to make debug more easy and more beginner friendly.
It is absolutely no work to implement this. If this would be much work to implement and maintain this, I would not suggested it. But in this case, it's just 3 steeps:
__init__():__exit__()ask, if it should delete or notPrevious discussion
None
Linked PRs