Repository navigation
Allow delete=False for tempfile.TemporaryDirectory() #100131
Description
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 a delete=False to prevent deleting.
Pitch
Code using this could look like this:
import argparse
import tempfile
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--keep-temp-files", action="store_true", help="Don't delete temporary files")
args = parser.parse_args()
with tempfile.TemporaryDirectory(delete=not args.keep_temp_files) as tempdir:
# Do something
if args.keep_temp_files:
print(f"You can view your temporary files at {tempdir}")
if __name__ == "__main__":
main()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:
if args.keep_temp_files:
do_something("some/other/dir")
else:
with tempfile.TemporaryDirectory() as tempdir:
do_something(tempfile)This would need some code rewrite.
2. Using input()
with tempfile.TemporaryDirectory() as tempdir:
# Do something
if args.keep_temp_files:
print(f"You can view your temporary files at {tempdir}. Press enter to delete the files.")
breakpoint()This could cause problems in situations, where you can't easily give some Keyboard input to TTY.
3. Using a custom function
def my_temp_dir(delet: bool = false):
if delete:
return tempfile.TemporaryDirectory()
else:
return my_custom_handler()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:
- Take a additional argument in
__init__(): - Save is as class attribute
- In
__exit__()ask, if it should delete or not
Previous discussion
None
Activity
CAM-Gerlach commentedon Mar 19, 2023
See #69212 for the previous issue on this. FWIW, I was +/- 0 at the time, as I'd solved one of the previously-stated use cases in a cleaner fashion in #74168 , but I've come around to this idea as there are several other use cases for it presented there.
serhiy-storchaka commentedon Mar 23, 2023
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.
graingert commentedon Mar 23, 2023
there's one additional way to archive this:
JakobDev commentedon Mar 23, 2023
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.This is not suitable, if you are not a Python expert
gpshead commentedon Mar 23, 2023
I'm going to close this based on Serhiy's practical
mkdtemp()suggestion above. We've already got one way to do this.zware commentedon Mar 23, 2023
The 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.CAM-Gerlach commentedon Mar 24, 2023
FWIW, 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.
gpshead commentedon Mar 24, 2023
fair enough. I really don't think this feature will see much use, but I've used the PR to improve the docstring anyways.
AlexWaygood commentedon Mar 24, 2023
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:carljm commentedon Mar 24, 2023
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.gpshead commentedon Mar 24, 2023
It 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.
gh-100131: Add optional delete parameter to tempfile.TemporaryDirecto…
gpshead commentedon Mar 24, 2023
Thanks all, including the contributed PR and the discussion. Merged. You changed my mind. :)
pythongh-100131: Add optional delete parameter to tempfile.TemporaryD…
pythongh-100131: Add optional delete parameter to tempfile.TemporaryD…