Make a unique filesystem path, without creating the file

Matt Wheeler m at funkyhat.org
Sun Feb 14 18:58:47 EST 2016


On 14 Feb 2016 21:46, "Ben Finney" <ben+python at benfinney.id.au> wrote:
> What standard library function should I be using to generate
> ‘tempfile.mktemp’-like unique paths, and *not* ever create a real file
> by that path?

Could you use tempfile.TemporaryDirectory and then just use a consistent
name within that directory.

It's guaranteed not to exist because the directory was only just created
and only you can write to it? Has the added bonus of still being reasonably
secure, to appease people like Mr PointedEars.

(If you need multiple nonexistent paths in the same dir then perhaps use
tempfile.NamedTemporaryFile with your newly created temp dir and an
arbitrary suffix, and strip the suffix off to get the name you actually
use.)

--
Matt Wheeler
http://funkyh.at



More information about the Python-list mailing list