tempname.mktemp functionality deprecation

Tim Chase python.list at tim.thechases.com
Mon May 1 15:31:21 EDT 2017


On 2017-05-01 18:40, Gregory Ewing wrote:
> The following function should be immune to race conditions
> and doesn't use mktemp.
> 
> def templink(destpath):
>      """Create a hard link to the given file with a unique name.
>      Returns the name of the link."""
>      pid = os.getpid()
>      i = 1
>      while True:
>          linkpath = "%s-%s-%s" % (destpath, pid, i)
>          try:
>              os.link(destpath, linkpath)
>          except FileExistsError:
>              i += 1
>          else:
>              break
>      return linkpath

Ah, this is a good alternative and solves the problem at hand.

As a side-note, apparently os.rename() is only atomic on *nix
systems, but not on Windows.  For the time being, I'm okay with that.

Thanks for your assistance!

-tkc




More information about the Python-list mailing list