title: Copy a file, but don't overwrite, without TOCTTOU issues in Python
body: <p>I know that if I want to copy a file in Python but not overwrite the destination I can use code like this:</p>
  
  <pre><code>if os.path.exists(dest):
      raise Exception("Destination file exists!")
  else:
      shutil.copy2(src, dest)
  </code></pre>
  
  <p>But the state of the world could change between the time I call <code>os.path.exists</code> and the time I call <code>copy2</code>. Is there a more preferred way to copy without overwriting, presumably wherein the copy operation will raise an exception if the destination already exists?</p>