Page 157 -
P. 157
persistence
Grab your pencil and rewrite this try/except/finally code to use
with instead. Here’s your code with the appropriate finally
suite added:
try:
man_file = open('man_data.txt', 'w')
other_file = open('other_data.txt', 'w')
print(man, file=man_file)
print(other, file=other_file)
except IOError as err:
print('File error: ' + str(err))
finally:
if 'man_file' in locals():
man_file.close()
if 'other_file' in locals():
other_file.close()
Write your
“with” code
here.
you are here 4 121