URL: http://effbot.org/lib/tempfile.html

I learnt something useful today which I can't explain. When you use the tempfile module in the Python standard library and the mkstemp() function you get two things back: an integer and a string.:

>>> import tempfile
>>> x, y = tempfile.mkstemp()
>>> x, type(x)
(3, <type 'int'>)
>>> y, type(y)
('/tmp/tmpK19sIx', <type 'str'>)
>>> help(tempfile.mkstemp)

I don't know what to do with the integer so I just ignore it. I thought I would get the result of open("/tmp/tmpK19sIx","w+b").

The help section of mkstemp says:

mkstemp(suffix='', prefix=tmp, dir=None, text=False)
mkstemp([suffix, [prefix, [dir, [text]]]])
User-callable function to create and return a unique temporary
file. The return value is a pair (fd, name) where fd is the
file descriptor returned by os.open, and name is the filename.

What does os.open do? My guess is that it's a lower level constructor than builtin open().

All I wanted to do was generate a temporary filename where I can place some binary content to be unpacked by another little script that uses tarfile. I had to do something like this:

def TGZFileToHTML(content):
    __, tmp_name = tempfile.mkstemp(suffix='.tgz')
    open(tmp_name, 'w+b').write(content)
    result = tarfile2html(tmp_name)
    return result

Is this the right way to do it? I first looked at tempfile.mktemp() but it's apparently not secure.


Post your own comment

Pass the fd number as the first arg to the write call - write(fd, content). Details are here http://www.python.org/doc/2.4.2/lib/os-fd-ops.html

Richard Moore

Unfortunately you've just built your own (equally insecure) equivalent to mktemp(). The returned file descriptor is already openned, allowing you avoid a race condition whereby someone else could create a file with the same name. You can create a file object from the fd with a call to fdopen.

Hopefully Not Overbearing Guy

The function name is taken from a function in the C standard library which returns a file descriptor. Returning the filename also is a Python freebie. It is a good idea to give a book like Johnson and Troan's Linux Application Development or Richard Steven's Advanced Programming in the UNIX Environment a read, to get the background on this and the file descriptor mechanism, even if you aren't planning to do any C programming if you can avoid it; a lot of stuff in Python's os, sys socket, etc. modules (not to mention Perl/Ruby/whatever modules) directly reflect that heritage. (Also, the C function has a manpage: man mkstemp.)


Use the Tempfile class! It will take care of everything for you and return a familiar file-like object.

The lower level C library wrappers are not really necessary most of the time.

And Richard pointed it out but it deserves to be mentioned again. DO NOT THROW AWAY the integer returned by mkstemp if you choose to use it.

Really, don't.


tmp_fd, tmp_name = tempfile.mkstemp(suffix='.tgz')
f = os.fdopen(tmp_name, 'w+b')


NM... Follow Richard's suggestion.


# typo...
f = os.fdopen(tmp_fd, 'w+b')

Your email will never ever be published.

Related posts