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.32

Answer

Yes, we can replace the code with the provided code.

Work Step by Step

Let's look closely at the code 1$\hspace{4mm}$try: 2$\hspace{8mm}$while not end_of_file: 3$\hspace{12mm}$print(pickle.load(infile), end = " ") 4$\hspace{4mm}$except EOFError: 5$\hspace{8mm}$print("\nAll objects are read") 6$\hspace{4mm}$finally: 7$\hspace{8mm}$infile.close() # Close the input file Now, the execution will start from Line 1, it will run the try block first and will start reading the content of the file and printing it. When the EOF of the file will be reached, then it will raise EOFError which will be matched by the except EOFError block on line 4 and in the end finally block will close the file(line 6).
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.