Detect existing files?

Stephan Schulz schulz at Informatik.TU-Muenchen.DE
Wed May 2 16:05:46 EDT 2001


In article <m3r8y77hax.fsf at atrus.jesus.cam.ac.uk>,
Michael Hudson  <mwh at python.net> wrote:
>Stephan Schulz <schulz at informatik.tu-muenchen.de> writes:
>
>> I need the opposite case - if the file exists, I leave it alone,
>> otherwise I create it. 
>
>Then you want something along the lines of
>
>os.fdopen(os.open(filepath, os.O_RDWR|os.O_CREAT|os.O_EXCL, 0700), "r+w")
>
>you *don't* want to test if the file exists and then call open, 'cause
>then you have a race condition.
>
>If you're creating temporary files, you want to use tempfile in the
>standard library. which contains much cleverness intended to get it
>right.

Thanks again. But all of this is overkill for my application. What I
am doing is to use a template file and a list of instantiations to
create instances of the template file. These are short and simple text
files (control files for a distribution script for my theorem prover
with different parameters), and they are created exactly once (for
each template file), and
under my direct control.

I am aware of the extra distance you have to cover for critical
temporary files in a multi-user system, it just does not apply to me.

>> Moreover, I'm not yet into exception handling.
>
>Here, you don't really have a choice.  But relax; it's easy.

I managed this application without them (and I have handled them in
other languages, although rarely). If I need them, I'll dig deeper
into the dead trees.

Bye,

    Stephan

-- 
-------------------------- It can be done! ---------------------------------
   Please email me as schulz at informatik.tu-muenchen.de (Stephan Schulz)
----------------------------------------------------------------------------



More information about the Python-list mailing list