I am supposed to read from two files. One contains numbers (with invalid characters mixed in) and the other operators. The program stops after an exception is reached but I need it to continue to read the numbers file. I have tried the pass keyword with no luck.
try:
with open('numbers.txt') as file1, open('operators.txt') as file2:
for no1, no2, op in itertools.zip_longest(file1, file1, file2):
result = eval(no1.rstrip() + op.rstrip() + no2.rstrip())
print(no1.rstrip() + op.rstrip() + no2.rstrip() + ' = ' + str(result))
except IOError:
print('File cannot be found or opened')
exit()
except ZeroDivisionError:
print(no1.rstrip() + op.rstrip() + no2.rstrip() + ' - Division by 0 is not allowed')
except NameError:
print(no1.rstrip() + op.rstrip() + no2.rstrip() + ' - Cannot perform operation with characters')
I would really appreciate any help.