Processing files is more complex. In this example we open the file, read the first line which is a number and use that for division. Running this code will stop in the middle with a ZeroDivisionError exception. Even though we wrapped the code in a try: statement, this only caught the excepting we listed there.
from __future__ import print_function def read_file(f): print("before " + f) try: with open(f, 'r') as fh: number = int(fh.readline()) print(30 / number) except Exception: print(" there was a problem in " + f) print("after " + f) # 3 of the 4 file exist files = 'one.txt', 'zero.txt', 'two.txt', 'three.txt' for f in files: read_file(f) # before one.txt # 30 # after one.txt # before zero.txt # there was a problem in zero.txt # after zero.txt # before two.txt # there was a problem in two.txt # after two.txt # before three.txt # 10 # after three.txt
But what was the problem?