File IO.

Raymond Hettinger vze4rx4y at verizon.net
Thu Jan 2 02:42:42 EST 2003


"Jc" <jciber16 at hotpop.com> wrote in message
news:3e13e820_4 at corp.newsgroups.com...
> I'm writing a program and when it can't open a txt file it crashes. How
can
> 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.read()
> in_file.close()
>
> Thanks.
>
> JC

Wrap the call in a try/except block:

try:
    open('nothere.txt')
except IOError:
    print 'Cannon open file'


Raymond Hettinger






More information about the Python-list mailing list