tempfile in Python standard library

07 February 2006   7 comments   Python


Powered by Fusion×

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.


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

Sexy furniture 05 February 2006
Geeking with Eterm and Tkinter 09 February 2006
Related by keywords:
To readline() or readlines() 12 March 2004
bool is instance of int in Python 05 December 2008
Reciprocal lesson about gender perspectives 02 September 2011
Nginx vs. Squid 17 March 2009
IssueTrackerProduct now officially abandoned 30 March 2012
How and why to use django-mongokit (aka. Django to MongoDB) 08 March 2010
On the command line no one can hear you screen. Or can they? 03 May 2012
Nasty surprise of Django cache 09 December 2008
Random ID generator for Zope 02 September 2005
Google Calendar, iCalendar Validator but not bloody Apple iCal 09 April 2009
In Django, how much faster is it to aggregate? 27 October 2010
Google and Python code 22 February 2006