[Python-Dev] Re: NamedTemporaryFile and context managers

2021年4月08日 13:51:36 -0700

On Thu, 8 Apr 2021 13:31:26 -0700
Ethan Furman <[email protected]> wrote:
> 
> ```python
> from tempfile import NamedTemporaryFile
> 
> with NamedTemporaryFile() as fp:
> fp.write(b'some data')
> fp.close() # Windows workaround
> fp.open()
> data = fp.read()
> 
> assert data == 'some_data'
> ```
> 
> The problem is that, even though `fp.open()` is still inside the context 
> manager, the `close()` call deletes the file 
> [2]. To handle this scenario, my proposal is two-fold:
> 
> 1) stop using the TEMPFILE OS attribute so the OS doesn't delete the file on 
> close
> 2) add `.open()` to NamedTemporaryFile
Instead, you could add a dedicated `.reopen()`?
Regards
Antoine.
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/HUGSC2FB6VW3VFKGDYKUYUKNJEWNPF5T/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to