Python equivalent of _tempnam?

Robin Munn rmunn at pobox.com
Thu Apr 3 05:16:40 EST 2003


Dang Griffith <dmgriffith at tasc.com> 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
'/tmp/fileAjhgek'
>>> os.tempnam("", "foo")
'/tmp/foo6x64Kw'
>>> os.tempnam("", "foo")
'/tmp/fooKusOXN'
>>> os.tempnam(".", "foo")
'./fooxT9AB8'

-- 
Robin Munn <rmunn at pobox.com>
http://www.rmunn.com/
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838




More information about the Python-list mailing list