tempname.mktemp functionality deprecation

Cameron Simpson cs at zip.com.au
Sun Apr 30 18:41:43 EDT 2017


On 30Apr2017 06:52, Tim Chase <python.list at tim.thechases.com> wrote:
>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?

I'd be tempted to. But...

>> > - use a GUID-named temp-file instead for less chance of collision?

You could, but mktemp is supposed to robustly perform that task, versus "very 
very probably".

>> > - I happen to already have a hash of the file contents, so use
>> >   the .hexdigest() string as the temp-file name?

Hashes collide. (Yes, I know that for your purposes we consider that they 
don't; I have a very similar situation of my own). And what if your process is 
running twice, or leaves around a previous temp file by accident (or 
interruption) _or_ the file tree contains filenames named after the hash of 
their content (not actually unheard of)?

>> > - some other solution I've missed?

What about some variation on:

  from tempfile import NamedTemporaryFile
  ...
  with NamedTemporaryFile(dir=your_target_directory) as T:
      use T.name, and do your rename/unlink in here

Cheers,
Cameron Simpson <cs at zip.com.au>



More information about the Python-list mailing list