Python equivalent of _tempnam?

Mathias Waack mathias-usenet at valpo.de
Wed Apr 2 05:13:31 EST 2003


"Richard" <richardd at hmgcc.gov.uk> wrote:

> 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( ). 

Where is this function defined? There exists a function tmpnam(), 
which use is depracted and dangerous. Maybe you're talking about 
mkstmp(), which _can_ work in nearly the same manner as described 
above. 

> However I want to write this in Python, so is there
> an equivalent function? 

Maybe you should look into the tempfile module. 

> 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. 

Which offers a great starting point for nearly all kinds of race 
conditions;)

Mathias




More information about the Python-list mailing list