Detect existing files?
Michael Hudson
mwh at python.net
Wed May 2 14:10:46 EDT 2001
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.
> Moreover, I'm not yet into exception handling.
Here, you don't really have a choice. But relax; it's easy.
Cheers,
M.
--
In case you're not a computer person, I should probably point out
that "Real Soon Now" is a technical term meaning "sometime before
the heat-death of the universe, maybe".
-- Scott Fahlman <sef at cs.cmu.edu>
More information about the Python-list
mailing list