Testing for file existence

jay.krell at cornell.edu jay.krell at cornell.edu
Tue Oct 3 19:36:28 EDT 2000


Opening a file is also a good way to test for its existance. If you write
code like:
if exists:
    open

the file can be deleted between the existance check and the open, but
opening in the first place does not have this problem -- well, basically you
need to be prepared for open-for-read to fail because the file may not exist
and open-new-not-existing-for-write to fail bcause the file may exist,
regardless of checks beforehand.

 - Jay

-----Original Message-----
From: Aahz Maruch <aahz at panix.com>
Newsgroups: comp.lang.python
To: python-list at python.org <python-list at python.org>
Date: Tuesday, October 03, 2000 4:11 PM
Subject: Re: Testing for file existence


>In article <G1vHJE.E5u at bath.ac.uk>, P Browning <glpb at eis.bris.ac.uk> wrote:
>>
>>What's the recommended way to do this in Python?
>>In Perl you've got -x File tests.
>
>os.path.exists() and similar functions.
>--
>                      --- Aahz (Copyright 2000 by aahz at pobox.com)
>
>Androgynous poly kinky vanilla queer het    <*>
http://www.rahul.net/aahz/
>Hugs and backrubs -- I break Rule 6
>
>There's a difference between a person who gets shit zie doesn't
>deserve and a person who gets more shit than zie deserves.  --Aahz
>--
>http://www.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list