[Python-Dev] Remove tempfile.mktemp()

Chris Angelico rosuav at gmail.com
Tue Mar 19 09:37:56 EDT 2019


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.

>>> f = tempfile.NamedTemporaryFile(suffix=".flac")
>>> subprocess.check_call(["timidity", "-OF", "-o", f.name, "Music/gp_peers.mid"])
... snip ...
Wrote 29645816/55940900 bytes(52.9949% compressed)
>>> data = f.read()
>>> len(data)
29645816

ChrisA


More information about the Python-Dev mailing list