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

Answer

Here's an example of how you could write an object to a file and read it back(provided in the step-by-step procedure):

Work Step by Step

import pickle # Write an object to a file def write_object(obj, file_name): with open(file_name, "wb") as f: pickle.dump(obj, f) # Read an object from a file def read_object(file_name): with open(file_name, "rb") as f: obj = pickle.load(f) return obj # Define the object you want to write to the file obj = [1, 2, 3, "Hello World!"] # Invoke the write_object function to write the object to a file write_object(obj, "file.pickle") # Invoke the read_object function to read the object from the file obj_from_file = read_object("file.pickle") # Print the object read from the file print(obj_from_file) This will output: [1, 2, 3, "Hello World!"] So, you can see that the object has been successfully written to the file and read back.
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.