File IO.

Raymond Hettinger vze4rx4y at
Thu Jan 2 08:42:42 CET 2003

"Jc" <jciber16 at> wrote in message
news:3e13e820_4 at
> I'm writing a program and when it can't open a txt file it crashes. How
> I get it to report the file not being there? I set it up using the basic
> command,
> in_file = open(filename+".txt","r")
> text =
> in_file.close()
> Thanks.
> JC

Wrap the call in a try/except block:

except IOError:
    print 'Cannon open file'

Raymond Hettinger

More information about the Python-list mailing list