[Tutor] Equivalent exception of os.path.exists()

Tim Golden mail at timgolden.me.uk
Mon Nov 30 21:11:32 CET 2009

biboy mendz wrote:
> Lie and Tim's input are true that raw_input doesnt do anything or you 
> cant catch exception error from it. And i'm wrong in placing the 
> try-except clause, it should be on the fobj-open line. But im still 
> looking for the exception that will be raised when i input a filename 
> and that file already exists. I hope you get what i mean :-)

There isn't one. The string you input which is a filename is just
a string. There's no reason for Python to check whether it is a
valid filename, any more than to check it is a valid integer or
a valid recipe on goodrecipes.com. You have to decide what you
want to do with the string.

Then you do that (try to open it as a file; try to convert it
to a number; try to use it as a URL to goodrecipes.com) and
deal with exceptions appropriately.


More information about the Tutor mailing list