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

Answer

In Python, you can use the pickle module to serialize and deserialize objects to and from a file.

Work Step by Step

To open a file for writing objects, you can use the following code: import pickle # Write objects to a file with open("file.pickle", "wb") as f: $\hspace{4mm}$pickle.dump(obj, f) Where obj is the Python object you want to write to the file. To open a file for reading objects, you can use the following code: import pickle # Read objects from a file with open("file.pickle", "rb") as f: $\hspace{4mm}$obj = pickle.load(f) This will deserialize the objects stored in the file and return them as a Python object. Note that the "wb" mode opens the file in binary write mode, and the "rb" mode opens the file in binary read mode.
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.