title: How does using the try statement avoid a race condition?
body: <p>When determining whether or not a file exists, how does using the try statement avoid a "race condition"?</p>
  
  <p>I'm asking because a highly upvoted <a href="https://stackoverflow.com/a/85237/1217270">answer</a> (update: it was deleted) seems to imply that using <code>os.path.exists()</code> creates an opportunity that would not exist otherwise. </p>
  
  <p>The example given is:  </p>
  
  <pre><code>try:
     with open(filename): pass
  except IOError:
     print 'Oh dear.'
  </code></pre>
  
  <p>But I'm not understanding how that avoids a race condition compared to:</p>
  
  <pre><code>if not os.path.exists(filename):
      print 'Oh dear.'
  </code></pre>
  
  <p>How does calling <code>os.path.exists(filename)</code> allow the attacker to do something with the file that they could not already do?</p>