Opening and Closing Files in Python
The essential saving pattern is pickle.dump(object, file) with the file opened in wb mode. The object is serialized into bytes for storage.
Try it: Reading and Writing Text Files
How open() returns a file object with a position pointer, how read(), read(n), readline(), readlines() and for line in fh advance it (and return '' at the end), how 'w' and 'a' modes change the file, and why a closed file (after close() or a with block) can no longer be read.
How it works
- open(name, 'r') starts the pointer at 0; 'w' empties the file first; 'a' starts at the end.
- read() returns everything from the pointer to the end; read(n) at most n characters; readline() up to and including the next \n; each moves the pointer past what it returned.
- At the end of the file every read returns '' (an empty string), which is how loops know to stop; for line in fh reads line by line the same way.
- write() text waits in a buffer and reaches the file when it is closed (or the program ends); reading a 'w'/'a' file or writing an 'r' file raises io.UnsupportedOperation.
- close(), or leaving a with block, closes the file; any further read raises ValueError: I/O operation on closed file.
Default run (10 steps): Ready: notes.txt holds 4 lines (51 characters). Nothing is open yet. … Finished. notes.txt now holds "From: stephen\nSubject: meeting\nFrom: louis\nThanks!\n".
Simplified: The lab's own small Python interpreter runs a fixed, hand-written script (you choose the variant and the values, never the code); nothing is executed as real Python. Every script was run in CPython 3.12 with the same inputs and the lab reproduces its output and error messages. One virtual text file with \n line endings (as on Linux/macOS), positions counted in characters (ASCII only), and a write buffer that is only flushed by close() or the end of the program.
Loading the simulation…
From Memory to Storage
A Python object normally exists in your program's memory. The pickle module provides a way to convert that object into a byte stream for storage in a file. Later, pickle can retrieve the stored data and restore it into memory.
What do you think happens?
The variable shoplist is created, saved to a file, then deleted. Later, the file is loaded into a new variable called storedlist. What do you expect storedlist to contain?
Reveal answer
The saved list
del shoplist removes the variable's reference from the program's memory. It does not affect the data that pickle.dump() already stored in the file.
The Object Lifecycle
The lifecycle has two directions. During saving, pickle.dump(object, file) converts a Python object into a byte stream and places that stream in the file. During retrieval, pickle.load(file) reads the stored data and brings the object back into memory. The variable name can change during this process: the original object may be called shoplist, while the restored object may be called storedlist.
Saving with dump
To save an object, open the file in binary write mode, wb. Then pass the object and the opened file to pickle.dump(). After the operation is complete, close the file. The binary mode matters because pickle works with byte streams rather than text data.
import pickle
shoplist = ["apples", "bread", "milk"]
file = open("shoplist.pkl", "wb")
pickle.dump(shoplist, file)
file.close()
del shoplist- 1Import the pickle module so its dump operation can be used.
- 2Create the shoplist object in memory.
- 3Open the file in binary write mode, wb.
- 4Pass the object and the opened file to
pickle.dump(). - 5Close the file after dumping is complete.
- 6Delete the shoplist variable from memory. The stored data in the file remains available.
The essential saving pattern is pickle.dump(object, file) with the file opened in wb mode. The object is serialized into bytes for storage.
Restoring with load
import pickle
file = open("shoplist.pkl", "rb")
storedlist = pickle.load(file)
file.close()
print(storedlist)- 1Import the pickle module so its load operation can be used.
- 2Open the same file in binary read mode, rb.
- 3Pass the opened file to
pickle.load(). - 4Assign the restored object to storedlist.
- 5Close the file after loading is complete.
- 6Print the restored object.
['apples', 'bread', 'milk']The original shoplist variable is no longer needed for loading. pickle.load() uses the data in the file and creates the restored object assigned here to storedlist. This demonstrates that the file persists independently of whether the original variable still exists in memory.
Choosing the File Mode
| Task | File mode | Pickle operation | Data direction |
|---|---|---|---|
| Save an object | wb | pickle.dump(object, file) | Object to file |
| Restore an object | rb | pickle.load(file) | File to object |
The binary mode must match the direction of the pickle operation.
Text modes are a common source of errors in pickle programs. The required modes for these operations are the binary modes wb and rb, because pickle stores and retrieves byte streams.
Mistakes to Avoid
Common mistake Opening the file in a text mode instead of a binary mode.
What it looks likeOpening the file for pickle operations without using wb when saving or rb when loading.
Why it failspickle converts objects into byte streams, so its file operations require the binary modes wb and rb.
Do this insteadUse wb with pickle.dump() and rb with pickle.load().Common mistake Using pickle.load() while trying to save an object.
What it looks likeCalling pickle.load(file) immediately after creating an object that needs to be stored.
Why it failspickle.load()retrieves data from a file; it does not perform the save direction.Do this insteadUse pickle.dump(object, file) to save an object.Common mistake Using pickle.dump() while trying to restore an object.
What it looks likeCalling pickle.dump() when the goal is to retrieve an object already stored in a file.
Why it failspickle.dump()saves an object to a file; it does not perform the retrieval direction.Do this insteadUse pickle.load(file) to restore the stored object.
Common mistake Forgetting to close the file after dumping.
What it looks likePerforming pickle.dump() and leaving the file open.
Why it failsForgetting to close files after dumping is identified as a common error.
Do this insteadComplete the pickle operation and close the file afterward.
Common mistake Assuming del removes the pickled data.
What it looks likeDeleting shoplist and expecting the saved file to become empty.
Why it failsdel removes the variable from memory, while the pickled data persists independently in the file.
Do this insteadTreat deletion of the variable and storage in the file as separate actions.
Check Your Understanding
You have a Python object named scores that must be saved to a file named scores.pkl and later restored into a variable named saved_scores. Write the two pickle operation calls and identify the correct binary mode for each file opening.
Hints
- Saving moves the object toward the file.
- Restoring moves stored data from the file into memory.
- Use wb for saving and rb for restoring.
Tracing the Direction
Which operation belongs in each blank: saving scores to scores.pkl or restoring saved_scores from scores.pkl?
- 1Saving
Open scores.pkl in wb mode, then use
pickle.dump(scores, file). The direction is from the Python object to the file. - 2Restoring
Open scores.pkl in rb mode, then use
saved_scores=pickle.load(file). The direction is from the file back to a Python object. - 3Closing
Close the file after the dump or load operation has been completed.
Saving uses wb with pickle.dump(); restoring uses rb with pickle.load().
Key Takeaways
- 1
pickle.dump(object, file)serializes a Python object into a byte stream and saves it to a file. - 2
pickle.load(file)retrieves stored pickle data and restores it into memory. - 3Use binary write mode wb for dumping and binary read mode rb for loading.
- 4Deleting a variable with del removes it from memory but does not remove the data already stored in the file.
- 5Close the file after completing the pickle operation.
At a glance
- pickle moves Python objects between memory and files by converting them to and from byte streams.
- Use
pickle.dump()with wb to save andpickle.load()with rb to restore. - A variable can be deleted after saving without removing the data from the file.
- Complete the dump or load operation before closing the file.