Python equivalent of _tempnam?

Robin Munn rmunn at
Thu Apr 3 12:16:40 CEST 2003

Dang Griffith <dmgriffith at> wrote:
> How about using os.tempnam(...) ?
>>>> import os
>>>> x = os.tempnam("", "output_")+".txt"
>>>> print x
> C:\TEMP\output_2.txt"
>>>> x = os.tempnam("", "output_")+".txt"
>>>> print x
> C:\TEMP\output_3.txt
> In my few minutes of spearminting, I couldn't get the first parameter
> (the directory) to behave the way I interpret the documentation as
> saying it should.  Could be my interpretation.  :-)  I'm using Win2k,
> Python 2.2.2.
>    --dang

[snip fully-quoted post under response. Please don't top-post.]

Looks like it's putting the "common location for temporary files" in
when the dir parameter is an empty string. My guess is that internally,
it's checking dir for truth; if dir is false, then the temp dir is used.
If you want temporary files in the current directory, try specifying a
dir parameter of "." -- see below for Unix example:

>>> os.tempnam()
__main__:1: RuntimeWarning: tempnam is a potential security risk to your program
>>> os.tempnam("", "foo")
>>> os.tempnam("", "foo")
>>> os.tempnam(".", "foo")

Robin Munn <rmunn at>
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838

More information about the Python-list mailing list