best way to check if a file exists?

Steven D'Aprano steve at
Thu Nov 2 14:10:55 CET 2006

On Thu, 02 Nov 2006 01:44:25 +1100, Ben Finney wrote:

> "wittempj at" <martin.witte at> writes:
>> Ben Finney wrote:
>> > "wittempj at" <martin.witte at> 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.

But there can be a race condition between os.path.exists returning True
and you trying to open the file, if some other process deletes or renames
the file in the meantime.


More information about the Python-list mailing list