Make a unique filesystem path, without creating the file

Cameron Simpson cs at
Mon Feb 15 18:05:39 EST 2016

On 15Feb2016 12:19, Ben Finney <ben+python at> wrote:
>Dan Sommers <dan at> writes:
>> On Mon, 15 Feb 2016 11:08:52 +1100, Ben Finney wrote:
>> > I am unconcerned with whether there is a real filesystem entry of
>> > that name; the goal entails having no filesystem activity for this.
>> > I want a valid unique filesystem path, without touching the
>> > filesystem.
>> That's an odd use case.
>It's very common to want filesystem paths divorced from accessing a
>filesystem entry.
>For example: test paths in a unit test. Filesystem access is orders of
>magnitude slower than accessing fake files in memory only, it is more
>complex and prone to irrelevant failures. So in such a test case
>filesystem access should be avoided as unnecessary.

But.. then why a filesystem path at all in that case? Why use a filesystem as a 
reference at all?

I've been watching this for a few days, and am struggling to understand your 
use case.

The only modes I can imagine for such a thing (a generated but unused filename) 

  checking that the name is syntactly valid, for whatever constrains you may 
  have (but if you're calling an opaque mktemp-like function, is this feasible 
  or remediable?)

  checking that the name generated does in fact not correspond to an existing 
  file (which presumes that the target directory has no other users, which also 
  implies that you don't need mktemp - a simple prefix+unused-ordinal will do)

  generating test paths using a real filesystem as a reference but not making a 
  test file - I'm having trouble imagining how this can be useful

  generating test paths without using a real filesystem as a reference, but 
  then you can't even use mktemp

I think I can contrive your test case scenario using #3:

  filepath = mktemp(existing_dir_path)
  fp = InMemoryFileLikeClassWithBogusName(filepath)
  do I/O on fp ...

but I don't see how it is useful to have a notion of a filepath at all in this 
case, and therefore I don't see why you would want a mktemp-like function 
available. Can you elaborate with a concrete example and its purpose which 
would work with a mktemp-ish official function?

You say:
>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
>I'm looking for how to get at that functionality in a non-deprecated
>way, without re-implementing it myself.

I think "the standard library clearly has this useful functionality 
implemented, but simultaneously warns strongly against its use" pretty much 
precludes this.

I think you probably need to reimplement. However if your intent is never to 
use the path you can use something very simple (my personal habit is 
prefix+ordinal where that doesn't already exist - keep the last ordinal to 
arrange a distinct name next time).

Cameron Simpson <cs at>

More information about the Python-list mailing list