[Python-Dev] Warn about mktemp once again?

Hrvoje Nikšić hrvoje.niksic at avl.com
Tue Apr 29 16:16:06 CEST 2008


On Tue, 2008-04-29 at 15:34 +0200, Giovanni Bajo wrote:
> > As of 2.6, Greg's use case is addressed by the new 'delete' parameter on 
> > tempfile.NamedTemporaryFile.
> 
> Then I personally don't have any objection to the removal of os.mkstemp.

os.mkstemp is still useful when you *don't* need the file object, but
the actual file descriptor, for passing to C code or to the child
processes, or to mmap.mmap and such.  It is also familiar to the Unix/C
hackers, and it should cost very little to keep it around.

> Since we're at it, a common pattern I use is to create temporary file to 
> atomically replace files: I create a named temporary file in the same 
> directory of the file I want to replace; write data into it; close it; 
> and move it (POSIX "move": rename with silent overwrite) to the 
> destination file. AFAIK, this is allows an atomic file replacemente on 
> most filesystems.
> 
> I believe this is a common useful pattern that could be handled in 
> module tmpfile (especially since the final "rename" step requires a 
> little care to be truly multiplatform).

I agree, having a tempfile class with rename-on-close semantics would be
very useful.



More information about the Python-Dev mailing list