title: Write file with specific permissions in Python
body: <p>I'm trying to create a file that is only user-readable and -writable (<code>0600</code>).</p>
  
  <p>Is the only way to do so by using <code>os.open()</code> as follows? </p>
  
  <pre><code>import os
  fd = os.open('/path/to/file', os.O_WRONLY, 0o600)
  myFileObject = os.fdopen(fd)
  myFileObject.write(...)
  myFileObject.close()
  </code></pre>
  
  <p>Ideally, I'd like to be able to use the <code>with</code> keyword so I can close the object automatically. Is there a better way to do what I'm doing above? </p>