61

tempfile.mkstemp() returns:

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.

How do I convert that OS-level handle to a file object?

The documentation for os.open() states:

To wrap a file descriptor in a "file object", use fdopen().

So I tried:

>>> import tempfile
>>> tup = tempfile.mkstemp()
>>> import os
>>> f = os.fdopen(tup[0])
>>> f.write('foo\n')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IOError: [Errno 9] Bad file descriptor
CraigTeegarden
  • 8,173
  • 8
  • 38
  • 43
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173

6 Answers6

60

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")
Peter Hoffmann
  • 56,376
  • 15
  • 76
  • 59
  • 1
    That works--thanks. But technically fdopen returns a file object (and you pass in a file descriptor), so if I could edit your answer I'd change it to "f = os.fdopen(tup[0], "w");f.write("foo") – Daryl Spitzer Oct 03 '08 at 20:03
  • If you don't use `os.fdopen`, you need to make sure you close the temp file handle with os.close(tup[0]) according to http://www.logilab.org/blogentry/17873. – deterb Feb 20 '15 at 18:17
13

Here's how to do it using a with statement:

from __future__ import with_statement
from contextlib import closing
fd, filepath = tempfile.mkstemp()
with closing(os.fdopen(fd, 'w')) as tf:
    tf.write('foo\n')
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
  • 5
    Is `closing` really necessary? `os.fdopen` returns a `TextIOWrapper` whose `__exit__` turned out to close it, tested on Python 3.5 (CPython). – Tim Diels Jun 20 '16 at 15:58
7

You forgot to specify the open mode ('w') in fdopen(). The default is 'r', causing the write() call to fail.

I think mkstemp() creates the file for reading only. Calling fdopen with 'w' probably reopens it for writing (you can reopen the file created by mkstemp).

efotinis
  • 14,565
  • 6
  • 31
  • 36
  • 2
    a temporary (hence empty) file ready only for reading would be useless. mkstemp returns the file opened in binary mode for both reading and writing, per the docstring. – jcomeau_ictx Jul 08 '21 at 00:11
4
temp = tempfile.NamedTemporaryFile(delete=False)
temp.file.write('foo\n')
temp.close()
glglgl
  • 89,107
  • 13
  • 149
  • 217
hoju
  • 28,392
  • 37
  • 134
  • 178
  • 2
    The "delete" parameter was added in version 2.6, so that won't work for older Python versions. – A B May 05 '10 at 01:11
2

What's your goal, here? Is tempfile.TemporaryFile inappropriate for your purposes?

Alex Coventry
  • 68,681
  • 4
  • 36
  • 40
0

I can't comment on the answers, so I will post my comment here:

To create a temporary file for write access you can use tempfile.mkstemp and specify "w" as the last parameter, like:

f = tempfile.mkstemp("", "", "", "w") # first three params are 'suffix, 'prefix', 'dir'...
os.write(f[0], "write something")
MartinD
  • 125
  • 3