Detect file existance before open?
greg andruk
meowing at banet.net
Sun Oct 17 01:04:43 EDT 1999
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.
More information about the Python-list
mailing list