Re: Python equivalent of _tempnam?

Dang Griffith dmgriffith at tasc.com
Wed Apr 2 16:00:37 EST 2003


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

Richard wrote:

> Hi,
> 
> I am writing an application which produces file output. Files are outputted
> to a directory and I want to ensure that if an output filename is given that
> already exists in that directory a number is affixed to the filename to
> ensure that the existing file is not appended to or overwritten. i.e.
> output_0.txt, then output_1.txt etc...
> 
> I have been told that a way of doing this in C is by using _tempnam( ).
> However I want to write this in Python, so is there an equivalent function?
> My application will only be executed occasionally so I am unable to keep an
> internal record of file numbering. I assume that I need to find the highest
> number file (if exists) in the directory and then increment that by one
> before creating the new file.
> 
> Any ideas?
> 
> Cheers
> 
> Richard
> 
> 





More information about the Python-list mailing list