But if it doesn't, the path name is no longer valid. Just exit if that happens.

mkdtemp makes the directory more or less securely. os.makedirs() does not.

mkdtemp creates the file read/writeable only by the user creating, os.makedirs() creates it 0777

os.makedirs creates sub paths (in this case, it could in theory make a /tmp on a system without it, and create it
open perms). There is also a race between the os.path.exists and the makedirs.