tempname.mktemp functionality deprecation

Gregory Ewing greg.ewing at canterbury.ac.nz
Mon May 1 02:40:19 EDT 2017


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

-- 
Greg



More information about the Python-list mailing list