Make a unique filesystem path, without creating the file

Gregory Ewing greg.ewing at
Mon Feb 15 03:00:39 EST 2016

Ben Finney wrote:
> One valid filesystem path each time it's accessed. That is, behaviour
> equivalent to ‘tempfile.mktemp’.
> My question is because the standard library clearly has this useful
> functionality implemented, but simultaneously warns strongly against its
> use.

But it *doesn't*, if your requirement is truly to not touch
the filesystem at all, because tempfile.mktemp() *reads* the
file system to make sure the name it's returning isn't
in use.

What's more, because you're *not* creating the file, mktemp()
would be within its rights to return the same file name the
second time you call it.

If you want something that really doesn't go near the file
system and/or is guaranteed to produce multiple different
non-existing file names, you'll have to write it yourself.


More information about the Python-list mailing list