Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 13 - Files and Exception Handling - Section 13.10 - Binary IO using Pickling - Check Point - MyProgrammingLab - Page 467: 13.31

Answer

The problem here is that the file will close before reading the data from the file.

Work Step by Step

Let's have a closer look at the code: 1while not end_of_file: 2$\hspace{4mm}$try: 3$\hspace{8mm}$print(pickle.load(infile), end = " ") 4$\hspace{4mm}$except EOFError: 5$\hspace{8mm}$end_of_file = True 6$\hspace{4mm}$finally: 7$\hspace{8mm}$infile.close() # Close the input file Now if we look closely at the lines 6-7, we can see finally block is closing the file and this block is inside the while loop. We know that finally block runs in every case, hence in the first iteration of the while block, the execution will reach at line 7 (since finally block always runs) and the file will get closed without reaching the EOF.
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.