Detect file existance before open?

Adrian Eyre a.eyre at optichrome.com
Mon Oct 18 09:41:39 EDT 1999


> 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?

What's wrong with:

try:
	f=open("afile", "r")
except IOError, val:
	print "Could not read file: " + val[1]

--------------------------------------------
Adrian Eyre <mailto:a.eyre at optichrome.com>
Optichrome Computer Solutions Ltd
Maybury Road, Woking, Surrey, GU21 5HX, UK
Tel: +44 1483 740 233  Fax: +44 1483 760 644
http://www.optichrome.com
--------------------------------------------





More information about the Python-list mailing list