best way to check if a file exists?

wittempj@hotmail.com martin.witte at gmail.com
Wed Nov 1 14:56:50 EST 2006



On Nov 1, 3:44 pm, Ben Finney <bignose+hates-s... at benfinney.id.au>
wrote:
> "witte... at hotmail.com" <martin.wi... at gmail.com> writes:
> > Ben Finney wrote:
> > > "witte... at hotmail.com" <martin.wi... at gmail.com> writes:
> > > > You could try to read the file, if that fails it doesn't exist:
>
> > > Except that there are other conditions than "File doesn't exist"
> > > that can cause an 'open' to fail.
>
> > Ok, true. You can test explicit on non existence as follows, and then
> > decide to open the fileOr you can simply use 'os.path.exists', as has been suggested several
> times in this thread.
>
os.path.exists() checks if a path exists, so a directory as argument
also returns True
> --
>  \      "The way to build large Python applications is to componentize |
>   `\          and loosely-couple the hell out of everything."  -- Aahz |
> _o__)                                                                  |
> Ben Finney




More information about the Python-list mailing list