title: Python: how to write to fd 3?
body: <p>in C, I can write to file descriptor 3 like this:</p>
  
  <pre><code>$ cat write.c 
  #include &lt;unistd.h&gt;
  
  int main(void) {
      write(3, "written in fd 3\n", 16);
  }
  </code></pre>
  
  <p>Then I can call the program and redirect fd 3 to fd 1 (stdin) like this:</p>
  
  <pre><code>$ ./write 3&gt;&amp;1
  written in fd 3
  </code></pre>
  
  <p>How can I do that in python?
  I checked <code>os.open()</code> but it creates a file descriptor out of a file in the filesystem (apparently I can't select which file descriptor to allocate) and <code>os.fdopen()</code> creates a file object out of a file descriptor (created with <code>os.open()</code>). So, how can I choose the file descriptor number.</p>
  
  <p>I tried:</p>
  
  <pre><code>with os.fdopen(3, 'w+') as fdfile:
  </code></pre>
  
  <p>but it gives me:</p>
  
  <pre><code>OSError: [Errno 9] Bad file descriptor
  </code></pre>
  
  <p>EDIT:
  This is my python program:</p>
  
  <pre><code>$ cat fd.py
  import os
  
  with os.fdopen(3, 'w+') as fdfile:
      fdfile.write("written to fd 3\n")
      fdfile.close()
  </code></pre>
  
  <p>And this is the result when I run it:</p>
  
  <pre><code>$ python fd.py 3&gt;&amp;1
  Traceback (most recent call last):
    File "fd.py", line 3, in &lt;module&gt;
      with os.fdopen(3, 'w+') as fdfile:
    File "/usr/lib/python3.8/os.py", line 1023, in fdopen
      return io.open(fd, *args, **kwargs)
  io.UnsupportedOperation: File or stream is not seekable.
  </code></pre>