Concepts / Opening and Closing Files in Python

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.

  • Programming
Interactive lab

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

  1. open(name, 'r') starts the pointer at 0; 'w' empties the file first; 'a' starts at the end.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

Educational simulation

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?

  • The saved list

  • An empty value because shoplist was deleted

  • Nothing because the file was deleted with the variable

Reveal answer
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

shoplistPython object in memorypickle.dump()converts object to bytesshoplist filestored byte streampickle.load()retrieves stored datastoredlistPython object in memoryobjectbyte streamstored datarestored object
How does a Python object move from memory into a file as bytes, and how does pickle.load() recreate it in memory?

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.

Code walkthrough
import pickle

shoplist = ["apples", "bread", "milk"]
file = open("shoplist.pkl", "wb")
pickle.dump(shoplist, file)
file.close()

del shoplist
  1. 1
    Import the pickle module so its dump operation can be used.
  2. 2
    Create the shoplist object in memory.
  3. 3
    Open the file in binary write mode, wb.
  4. 4
    Pass the object and the opened file to pickle.dump().
  5. 5
    Close the file after dumping is complete.
  6. 6
    Delete the shoplist variable from memory. The stored data in the file remains available.
Key point

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

Code walkthrough
import pickle

file = open("shoplist.pkl", "rb")
storedlist = pickle.load(file)
file.close()

print(storedlist)
  1. 1
    Import the pickle module so its load operation can be used.
  2. 2
    Open the same file in binary read mode, rb.
  3. 3
    Pass the opened file to pickle.load().
  4. 4
    Assign the restored object to storedlist.
  5. 5
    Close the file after loading is complete.
  6. 6
    Print the restored object.
Output
['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.

wbbinary write moderbbinary read modepickle.dump()object to filepickle.load()file to objectopens for savingopens for retrieving
What is the difference between wb and rb, and how does each mode control whether pickle saves or retrieves data?

Choosing the File Mode

TaskFile modePickle operationData direction
Save an objectwbpickle.dump(object, file)Object to file
Restore an objectrbpickle.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 like

    Opening the file for pickle operations without using wb when saving or rb when loading.

    Why it fails

    pickle converts objects into byte streams, so its file operations require the binary modes wb and rb.

    Do this instead
    Use wb with pickle.dump() and rb with pickle.load().
  • Common mistake

    Using pickle.load() while trying to save an object.

    What it looks like

    Calling pickle.load(file) immediately after creating an object that needs to be stored.

    Why it fails

    pickle.load() retrieves data from a file; it does not perform the save direction.

    Do this instead
    Use pickle.dump(object, file) to save an object.
  • Common mistake

    Using pickle.dump() while trying to restore an object.

    What it looks like

    Calling pickle.dump() when the goal is to retrieve an object already stored in a file.

    Why it fails

    pickle.dump() saves an object to a file; it does not perform the retrieval direction.

    Do this instead

    Use pickle.load(file) to restore the stored object.

  • Common mistake

    Forgetting to close the file after dumping.

    What it looks like

    Performing pickle.dump() and leaving the file open.

    Why it fails

    Forgetting to close files after dumping is identified as a common error.

    Do this instead

    Complete the pickle operation and close the file afterward.

  • Common mistake

    Assuming del removes the pickled data.

    What it looks like

    Deleting shoplist and expecting the saved file to become empty.

    Why it fails

    del removes the variable from memory, while the pickled data persists independently in the file.

    Do this instead

    Treat deletion of the variable and storage in the file as separate actions.

Check Your Understanding

EASY

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.
Worked example

Tracing the Direction

Which operation belongs in each blank: saving scores to scores.pkl or restoring saved_scores from scores.pkl?

  1. 1
    Saving

    Open scores.pkl in wb mode, then use pickle.dump(scores, file). The direction is from the Python object to the file.

  2. 2
    Restoring

    Open scores.pkl in rb mode, then use saved_scores = pickle.load(file). The direction is from the file back to a Python object.

  3. 3
    Closing

    Close the file after the dump or load operation has been completed.

Result

Saving uses wb with pickle.dump(); restoring uses rb with pickle.load().

Key Takeaways

  1. 1
    pickle.dump(object, file) serializes a Python object into a byte stream and saves it to a file.
  2. 2
    pickle.load(file) retrieves stored pickle data and restores it into memory.
  3. 3
    Use binary write mode wb for dumping and binary read mode rb for loading.
  4. 4
    Deleting a variable with del removes it from memory but does not remove the data already stored in the file.
  5. 5
    Close 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 and pickle.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.

More concepts starting with O