-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Prevent auto-deletion of NamedTemporaryFile
after construction
#135012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I don't think it's a common usecase and I think the user should be responsible at construction time for that. In the first place I don't think an automatically managed temporary file should be made if it can become a non-temporary one later. For that, I'd suggest opening a DPO thread first. |
tempfile
: provide means to change delete
and/or delete_on_close
properties of already existing NamedTemporaryFile
objectsNamedTemporaryFile
after construction
This was proposed multiple times in the past, but it cannot be reliably implemented. One reason -- if the file was created with flag O_TEMPORARY, it will be deleted after closing by the OS. This use case is resolved by using TemporaryDirectory and creating a file in a temporary directory. If you decided to keep the file, you can simply move it outside of the temporary directory. |
Sure? I mean that's effectively what many programs (like
Which is why I explicitly named
Well, not really solved, because one is then back to manually ensuring the cleanup - which is of course possible. But even if you say it should not be supported, it might make sense to add a sentence which tells that just moving the (temporary) file to another location - while effectively working right now, is not guaranteed to do so. |
Hey @calestyo -- if you end up opening a discussion at https://discuss.python.org/new, please link here. I'm interested in following the discussion. |
Closing this issue as a duplicate of #67943. |
Not sure whether there'd be a lot to discuss?! I mean the use case(s) are rather obvious, the implementation seems straightforward as it would be just exporting the already existing property via some API. But you can of course open such discussion yourself. :-) Thanks, [0] Sorry that I haven't found the duplicate, I was only searching for |
Uh oh!
There was an error while loading. Please reload this page.
Feature or enhancement
Proposal:
Hey.
Using the the automatic cleanup of
NamedTemporaryFile
simplifies handling of the created temporary files obviously a lot.There are however use cases, where sometimes it may be found after the named temp file has been created, that it actually shouldn’t be temporary but kept and maybe even moved to another name.
Consider that you want to update a local file from a downloaded source. You may first want to download from the remote location to some temporary file, then diff the two, and only if there are some differences, move the file to the local one (thereby overwriting that) and then do some extra processing of it.
Right now, there seems no documented/stable way of doing this:
k._closer.delete = False
but that is of course also not part of the public API.I think it would be nice to have these properties exported under some proper public name or - at least documented to be part of the usable API, despite the underscore.
Thanks,
Chris.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
The text was updated successfully, but these errors were encountered: