Put the code under the try or in a function and call the function under the try
def read_file(infile):
tex1 = infile.readline()
outfile = open ('Book11.txt', "w")
while tex1:
rfields = tex1.split()
# do something
print >> outfile, 'something'
outfile.close()
infile.close()
try:
infile = open ('Book1.txt', "r")
read_file(infile)
except IOError:
print "input file %s is missing" %'Book1.txt'
except:
print "Some other error"
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
else part of try is for the case of no exception happened, there is exit function and same you can do also by raising SystemExit, using function like above is also good idea to organize code. You can raise exception in function and handle it in calling function if you end except with raise.
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852