best way to check if a file exists?

sjdevnull at yahoo.com sjdevnull at yahoo.com
Wed Nov 1 09:59:23 EST 2006


Ben Finney wrote:
> "wittempj at hotmail.com" <martin.witte at gmail.com> writes:
>
> > Ben Finney wrote:
> > > "wittempj at hotmail.com" <martin.witte at gmail.com> writes:
> > > > You could try to read the file, if that fails it doesn't exist:
> > >
> > > Except that there are other conditions than "File doesn't exist"
> > > that can cause an 'open' to fail.
> >
> > Ok, true. You can test explicit on non existence as follows, and then
> > decide to open the file
>
> Or you can simply use 'os.path.exists', as has been suggested several
> times in this thread.

The O_CREAT|O_EXCL method pointed out elsethread is preferrable,
checking with "exists" and then creating if it returns false is racey.




More information about the Python-list mailing list