Detect file existance before open?

Aahz Maruch aahz at netcom.com
Sun Oct 17 01:32:05 EDT 1999


In article <874sfqefx0.fsf at slip-32-101-160-199.ma.us.prserv.net>,
greg andruk  <meowing at banet.net> wrote:
>In nnmh:python, Gordon McMillan <gmcm at hypernet.com> wrote:
>> greg andruk wrote:
>>> In nnmh:python, Bill Rodgers <Bill at nospam.com> wrote:
>>> 
>>>> I would like to check for the existance of a file before I
>>>> attempt an open and maybe throw an exception.  I don't see a
>>>> way to do this.
>>> 
>>> os.stat() will do the job.
>
>>>>> os.stat('kablooie')
>> Traceback (innermost last):
>>   File "<interactive input>", line 1, in ?
>> OSError: [Errno 2] No such file or directory: 'kablooie'
>>>>> 
>
>Right, it throws an exception, just what was asked for.  And on
>success, the permissions and file/dir/etc. status are available.

Actually, Bill wrote confusingly, but his intention was to ask for a
method that *doesn't* throw an exception (the "and" combined "open" and
"exception"; it wasn't a sentence separator).  Isn't English fun?
Gordon gave the right answer.

(Not your fault, I thought that's what he meant the first read, too.)
--
                      --- Aahz (@netcom.com)

Androgynous poly kinky vanilla queer het    <*>      http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6  (if you want to know, do some research)




More information about the Python-list mailing list