This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: tempfile misses usecase which requirs renaming
Type: enhancement Stage:
Components: Library (Lib) Versions:
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: dallison, eric.araujo, georg.brandl, giampaolo.rodola
Priority: normal Keywords:

Created on 2005-12-25 20:41 by dallison, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg61216 - (view) Author: Dennis Allison (dallison) Date: 2005-12-25 20:41
tempfile provides a very convenient API but it seems to
miss one important use case.  For example, suppose the
program is stream editing the file.  Typically one reads 
the source file, edits the data, and writes it to a
temporary file.  Upon successful completion of the edit,
the program renames the temporary to be the original
source file, which is atomic in most operating systems,
 There does not appear to be any neat way to do this with 
the current API without the program throwing an
exception because of the deletion wrapper.  Perhaps a
"rename" method needs to be added to manage the delete
seamlessly.  I am not especially fond of that solution
as it adds new functionality to file descriptors.

msg67680 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2008-06-04 02:54
If you mean "renaming the temporary file while it is still opened" I see
2 reasons for not doing that:

- On Windows this is not possible since files remains locked as long as
you close() them.

- open().name and tempfile.NamedTemporaryFile().name attributes are not
reliable since they remains the same even after the file renaming.
>>> import os
>>> f = open('1', 'w')
>>> os.rename('1', '2')
msg114594 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-08-21 23:02
Closing due to questionable usecase.
msg114598 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-08-21 23:06
Maybe #8828 or #8604 are what you’re looking for.
Date User Action Args
2022-04-11 14:56:14adminsetgithub: 42728
2010-08-21 23:06:18eric.araujosetnosy: + eric.araujo
messages: + msg114598
2010-08-21 23:02:25georg.brandlsetstatus: open -> closed

nosy: + georg.brandl
messages: + msg114594

resolution: rejected
2008-06-04 02:54:17giampaolo.rodolasetnosy: + giampaolo.rodola
messages: + msg67680
2005-12-25 20:41:19dallisoncreate