tempname.mktemp functionality deprecation

Tim Chase python.list at tim.thechases.com
Sun Apr 30 07:52:17 EDT 2017


On 2017-04-29 20:51, Devin Jeanpierre wrote:
> On Sat, Apr 29, 2017 at 11:45 AM, Tim Chase wrote
> > So which route should I pursue?
> >
> > - go ahead and use tempfile.mktemp() ignoring the deprecation?
> >
> > - use a GUID-named temp-file instead for less chance of collision?
> >
> > - I happen to already have a hash of the file contents, so use
> >   the .hexdigest() string as the temp-file name?
> >
> > - some other solution I've missed?
> 
> I vote the last one: you can read the .name attribute of the
> returned file(-like) object from NamedTemporaryFile to get a path
> to a file, which can be passed to other functions.

Unfortunately, his entails the file-preexisting, causing the same
EEXISTS problem as before:

  $ cd ~/tmp
  $ echo hello > a
  $ python
  ...
  >>> from tempfile import NamedTemporaryFile as NTF
  >>> f = NTF(dir='.')
  >>> import os
  >>> os.link('a', f.name)
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  OSError: [Errno 17] File exists
  >>> f.name
  '/home/tim/tmp/tmpokEpht'

-tkc




More information about the Python-list mailing list