Detect file existance before open?

Tim Evans tre17 at student.canterbury.ac.nz
Sun Oct 17 04:50:15 EDT 1999


"Gordon McMillan" <gmcm at hypernet.com> writes:

> Bill Rodgers asks:
> 
> > 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.  I would like to tell the user that the file he
> > specified doesn't exist.  Is there a way to do this or do I need
> > to catch the exception and deal with it that way?
> 
> os.path.exists(nm), probably also os.path.isfile(nm).
> 
> Of course, the fact that it exists and is a file still doesn't mean 
> the user can open it.
> 
> 
> - Gordon

os.access() sould be mentioned as well, it can check that the user has
permission to open the file in whatever mode you want.  Provided
you're using Unix and Pthon 1.5.2, that is.

--
Tim Evans





More information about the Python-list mailing list