Reading and Writing Text Files in Python
Main memory (CPU and RAM) is where programs execute and store data while running, but it is volatile—everything is erased when power is lost.
The Vanishing Budget
Imagine a Python program that calculates a monthly budget. It stores income, expenses, and the remaining balance in variables, then displays the results on screen. The program works correctly, but after you close it, the calculations are no longer available. Where did the data go?
While the program was running, its calculations and variables existed in the computer's CPU and main memory. The CPU performed the work, while main memory held the data structures and intermediate results used by the program. This workspace is active and fast, but it is volatile. If power is lost, the data stored there is erased.
Where Program Data Lives
Main memory, including the CPU and RAM as described in this material, is the place where programs execute and store data while they are running. It is volatile because it requires electrical power; when power is lost, the data in that active workspace disappears.
Secondary memory includes files, hard drives, solid-state drives, USB flash drives, and other storage devices. It is non-volatile, so data written there remains available even when the power is off.
| Location | Role while a program runs | What happens without power |
|---|---|---|
| CPU and main memory | Executes the program and holds its active data | The data is erased |
| Secondary memory | Stores files and other persistent data | The data remains available |
The source distinguishes the temporary working area from persistent storage.
Files as Persistent Storage
A file is data stored in secondary memory. When a program explicitly writes information to a file, it moves that information out of the temporary workspace of main memory and into storage that survives the end of the program and the loss of electrical power.
Think of main memory as a whiteboard used while solving a problem. You can work on the whiteboard while the program is active, but its contents do not provide a lasting record when the active session ends. A file is like writing the final answer in a notebook and placing that notebook on a shelf. The notebook remains available for a later session.
Saving a Monthly Budget
A budget program calculates income, expenses, and a remaining balance. How can the results remain available after the program closes?
Calculate: While the program runs, the calculations and their results are held in variables in the CPU and main memory.
Write: The program explicitly writes the budget result to a file in secondary memory instead of only displaying it on screen.
Close: The program ends, so its active data in main memory is no longer available.
Read later: During a later run, the program can read the file and see the previously calculated budget result.
The result persists because it was written to a file in non-volatile secondary memory.
Text and Database Files
Text files contain plain text made from readable letters, numbers, and punctuation. When opened in a text editor such as Notepad or VS Code, their actual content can be viewed directly. Examples include CSV budget data, program log files, and configuration files.
Binary database files use a structured format intended for database software rather than direct reading in a text editor. A database file may organize many records in tables and include relationships, indexes for searching, and other internal structures. The database software acts as the intermediary through which you work with that file.
| Feature | Text file | Binary database file |
|---|---|---|
| Representation | Readable plain text | Structured binary format |
| Direct viewing | Can be read in a text editor | Not intended to be read in a text editor |
| Typical use | CSV data, logs, and configuration settings | Records, tables, relationships, and indexes |
| How it is accessed | A program can read and write the text | Database software is used as an intermediary |
Power Loss and Recovery
Power loss exposes the difference between temporary program state and persistent file data. Before power is removed, a running program may have calculations and variables in CPU and main memory, while a file may hold data in secondary memory. After power is removed, the active CPU and main-memory data is erased. Data that was written to secondary memory remains available.
Assuming that a value shown on screen has automatically been saved.
Displaying a result does not move it from the temporary program workspace into secondary memory.
Fix:
Explicitly write the data to a file when it must survive the program's execution.Expecting variables to remember their values during a later program run.
The variables belonged to the earlier running program and were held in volatile CPU and main memory.
Fix:
Save the required data in a file and read that file during the later run.Treating a binary database file as though it were a plain text document.
Binary database files use structured formats intended for database software.
Fix:
Use database software as the intermediary for interacting with the database file.
Persistence Practice
A game calculates a player's progress while running. Decide what must happen if the progress should still be available after the program closes. Then classify the storage involved at each stage: the active calculation, the saved record, and the later recovery.
Hints
- Ask where the active calculation is held while the program runs.
- Ask what action moves the progress into non-volatile storage.
- Ask how a later program run can obtain the saved progress.
Checking the Storage Path
A program calculates a result, writes it to a text file, loses power, and is started again. Which parts of the result can remain available?
During calculation: The active result is held in the CPU and main memory, where the running program executes.
After writing: The written copy is stored in a file in secondary memory.
After power loss: The active copy in volatile memory is erased, while the file remains available.
During recovery: The later program run can read the file and recover the stored result.
Only the copy written to the file is persistent across power loss and program runs.
Lasting Program State
- The CPU and main memory are the active workspace for a running program, but they are volatile.
- When power is lost, data held only in the CPU and main memory is erased.
- Secondary memory is non-volatile, so files stored there remain available without power.
- A program must explicitly write data to a file for that data to persist beyond the program's execution.
- Text files contain readable plain text, while binary database files use structured formats accessed through database software.
Key Takeaways
- Running programs use the CPU and main memory as a fast but volatile workspace.
- Power loss erases data that exists only in CPU and main memory.
- Files store data in non-volatile secondary memory, allowing it to persist across program runs.
- Text files hold readable plain text, whereas binary database files are structured for specialized database software.
- Reading and writing files connects temporary program activity with persistent stored data.