how do i check if a file exists with python?

Moshe Zadka moshez at
Sun Dec 31 16:39:21 CET 2000

On Fri, 29 Dec 2000 14:02:25 GMT, ullrich at (David C. Ullrich) wrote:
> On Fri, 29 Dec 2000 16:54:56 +0200 (IST), Moshe Zadka
> >Now, it may be that I'm wrong, in which
> >case just go ahead an use os.path.isfile
> It's certainly true the the second foo is better than the first,
> in fact yes the first is evil. But there are other reasons a person 
> might want to know whether a file exists (although probably
> things like foo are the most likely explanation.)

You seemed to miss the above part of my posting. While not having
used isfile, I've frequently used isdir to my advatage before.

> I sometimes make cumulative backups of files containing
> important stuff. I sort of need to determine that file.001,
> file.002 and file.003 already exist and file.004 does not
> before deciding what to name the current backup file.

That's *really* evil. What if there is another version of your
process running???  Damn!, just lost that backup file. In that
case, I'd go for a Maildir-like algorithm, which promises no colisions.
Actually, in that case, usually what you want is CVS, but that's
a matter for another day.

> (When I tell people they're asking the wrong question I
> get in trouble for it...)

You're safe with us, buddy.

as-long-as-you-pay-your-protection-fee-in-time-ly y'rs, Z.
Moshe Zadka <sig at>
This is a signature anti-virus. 
Please stop the spread of signature viruses!

More information about the Python-list mailing list