Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 01

Files and Streams — Data Persistency in Python

Learn why Python files exist: data persistency, the stream between memory and disk, and how text files differ from binary files.

Intermediate25 min
Files and streams hero Module 16 Lesson 1 data persistency

A Python object in memory disappears when the program ends. A file keeps that data so the next run can read it again.

Use the playground at the bottom of this page, then take the quiz.

What you will learn

  • Why programs need data persistency
  • How a file differs from an object in memory
  • What a stream is
  • How text files differ from binary files
  • Which apps are non-persistent and which apps save records

The idea

From a memory object to a saved record

Programs you have written so far read values, store them in objects, and print a result. Those objects live in main memory. When the process stops, the objects are gone. That style is a non-persistent application.

A persistent application stores the result so it is still there later. Industry storage usually takes one of two forms:

Storage What it is
Files Named locations that hold records
Databases Software such as PostgreSQL or SQLite that organizes many records

This module is the file path. A file is a collection of records in secondary storage. The file name is the label of that location.

A stream is the flow of data between an object in memory and a file. Writing sends the object out to the file. Reading brings a record back into an object.

Python treats every file as text unless you say otherwise.

Kind Marker What it holds Typical names
Text t (the default) letters, digits, and symbols .py, .txt, .csv, .json
Binary b bytes, not decoded characters images, audio, pickle files

Text mode is the default, so "r" means "rt". Binary mode must be requested with b, as in "rb" or "wb".

Exception handling still applies here. A missing file raises FileNotFoundError. You will catch that in later lessons.

Example

python
note = "Workshop starts at 9\n"
with open("notebook.txt", "w", encoding="utf-8") as handle:
    handle.write(note)

with open("notebook.txt", "r", encoding="utf-8") as handle:
    print(handle.read())

The first block sends a string through a text stream into notebook.txt. The second block reads that record back. The playground uses the same pattern, with a short label you can change.

Text files store characters. Binary files, pickling, CSV, JSON, and folder operations come in the following lessons.

Key takeaways

  • Persistency means the data is still available after the program stops

  • A file is a named collection of records

  • A stream is the flow between memory and that file

  • Text mode (t) is the default; binary mode needs b

  • Non-persistent apps only print; persistent apps also store records

  • Previous: Custom Exceptions and raise

  • Next: File Opening Modes

Loading playground…

Check your understanding

Files and streams quiz

A file is best described as:

1 / 10