<p><a href="http://www.python.org/doc/2.5.2/lib/module-tempfile.html" rel="noreferrer">tempfile.mkstemp()</a> returns:</p>

<blockquote>
  <p>a tuple containing an OS-level handle to an open file (as would be returned by os.open()) and the absolute pathname of that file, in that order.</p>
</blockquote>

<p>How do I convert that OS-level handle to a file object?</p>

<p>The <a href="http://www.python.org/doc/2.5.2/lib/os-fd-ops.html" rel="noreferrer">documentation for os.open()</a> states: </p>

<blockquote>
  <p>To wrap a file descriptor in a "file
  object", use fdopen().</p>
</blockquote>

<p>So I tried:</p>

<pre><code>&gt;&gt;&gt; import tempfile
&gt;&gt;&gt; tup = tempfile.mkstemp()
&gt;&gt;&gt; import os
&gt;&gt;&gt; f = os.fdopen(tup[0])
&gt;&gt;&gt; f.write('foo\n')
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in ?
IOError: [Errno 9] Bad file descriptor
</code></pre>

You can use
```
```
os.write(tup[0], "foo\n")
```
```
to write to the handle.
If you want to open the handle for writing you need to add the
"w"
mode
```
```
f = os.fdopen(tup[0], "w")
f.write("foo")
```
```