[Python-Dev] Remove tempfile.mktemp()

Antoine Pitrou solipsis at pitrou.net
Tue Mar 19 09:51:27 EDT 2019


On Wed, 20 Mar 2019 00:37:56 +1100
Chris Angelico <rosuav at gmail.com> wrote:
> On Wed, Mar 20, 2019 at 12:25 AM Antoine Pitrou <solipsis at pitrou.net> wrote:
> >
> >
> > -1.  Please don't remove tempfile.mktemp().  mktemp() is useful to
> > create a temporary *name*.  All other tempfile functions create an
> > actual file and impose additional burden, for example by making the
> > file unaccessible by other processes.  But sometimes all I want is a
> > temporary name that an *other* program will create / act on, not Python.
> > It's a very common use case when writing scripts.
> >
> > The only reasonable workaround I can think of is to first create a
> > temporary directory using mkdtemp(), then use a well-known name inside
> > that directory.  But that has the same security implications AFAICT,
> > since another process can come and create the file / symlink first.  
> 
> Can't you create a NamedTemporaryFile and permit the other program to
> use it? I just tried that (with TiMidity, even though it's quite
> capable of just writing to stdout) and it worked fine.

Does it always work? According to the docs, """Whether the name can be
used to open the file a second time, while the named temporary file is
still open, varies across platforms (it can be so used on Unix; it
cannot on Windows NT or later)""".

tempfile.mktemp() is portable.

Regards

Antoine.




More information about the Python-Dev mailing list