title: python os.fdopen(os.open()) can't be used for writing?
body: <p>This question has to do with the answer to <a href="https://stackoverflow.com/questions/5624359/write-file-with-specific-permissions-in-python">Write file with specific permissions in Python</a> for opening a file for writing (in python) with specific permissions.</p>
  
  <p>The code in the answer looks like:</p>
  
  <pre><code>with os.fdopen(os.open('foo', os.O_APPEND | os.O_CREAT, 0o644)) as out:
    out.write("hello\n")
  </code></pre>
  
  <p>This code in 2.7.1 (my company does not have 2.7.3 installed) produces:</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;stdin&gt;", line 2, in &lt;module&gt;
  IOError: File not open for writing
  </code></pre>
  
  <p><code>os.fdopen</code> has its own mode argument, but setting that doesn't help:</p>
  
  <pre><code>&gt;&gt;&gt; with os.fdopen(os.open('foo', os.O_APPEND | os.O_CREAT, 0o644), 'a') as out:
  ...   out.write("hello\n")
  ...
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  OSError: [Errno 22] Invalid argument
  </code></pre>
  
  <p>Long story short, I have not been able to figure out how to actually write to a file that has been opened via <code>os.fdopen</code> and <code>os.open</code>.  Any ideas?  Known bug in 2.7.1?</p>
  
  <p>Thanks in advance!</p>