Make a unique filesystem path, without creating the file

Gregory Ewing greg.ewing at canterbury.ac.nz
Mon Feb 15 03:11:01 EST 2016


Ben Finney wrote:

> The existing behaviour of ‘tempfile.mktemp’ – actually of its internal
> class ‘tempfile._RandomNameSequence’ – is to generate unpredictable,
> unique, valid filesystem paths that are different each time.

But that's not documented behaviour, so even if mktemp()
weren't marked as deprecated, you'd still be relying on
undocumented and potentially changeable behaviour.

> What I'm
> looking for is a way to use it (not re-implement it) that is public API
> and isn't scolded by the library documentation.

Then you're looking for something that doesn't exist,
I'm sorry to say, and it's unlikely you'll persuade
anyone to make it exist.

If you want to leverage stdlib functionality for this,
I'd suggest something along the lines of:

   def fakefilename(dir, ext):
     return os.path.join(dir, str(uuid.uuid4())) + ext

-- 
Greg



More information about the Python-list mailing list