Introduction to File Handling

In your programming journey so far, most of the data you’ve worked with has been volatile. This means once you stop your Python program, all your variables, lists, and dictionaries disappear! File handling is the bridge that allows us to save data permanently (persistence) on a storage device. Whether it is saving a high score in a game or storing a list of student records, file handling is an essential skill in Module 2: Data Structures and Algorithms.

Did you know? Almost everything you interact with on a computer is a file—from the Python script you write to the images you see on the web!


1. Working with Text Files

The syllabus focuses on text files. These are files where data is stored as a sequence of characters, usually readable by humans using a simple text editor like Notepad++.

The Life Cycle of a File

Think of working with a file like reading a physical book. To interact with it, you must follow three steps:

  1. Open the file: You take the book off the shelf and open it to a specific mode (reading or writing).
  2. Process the file: You read the words or write new notes on the pages.
  3. Close the file: You close the book and put it back. If you don't close it, it stays "locked" in your hands, and others might not be able to use it!

2. Opening and Closing Files in Python

In Python, we use the open() function. It generally takes two arguments: the filename and the mode.

Common File Modes

  • 'r' (Read): The default mode. Opens a file for reading. If the file doesn't exist, Python will throw an error.
  • 'w' (Write): Opens a file for writing. Warning: If the file already exists, it will delete everything inside and start fresh (overwrite)! If it doesn't exist, it creates a new one.
  • 'a' (Append): Opens a file to add data to the end. It does not delete what is already there.

The "Best Practice" Way: The with Statement

While you can use \( f = open("data.txt", "r") \) and \( f.close() \), it is easy to forget to close the file. The with statement automatically closes the file for you, even if an error occurs.

Example of safe file handling:

with open("example.txt", "w") as f:
    f.write("Hello Computing students!")

Quick Review: Always use the with statement to ensure your files are closed properly and memory is managed efficiently.


3. Reading from Text Files

There are three main ways to read data from a file in Python:

  • f.read(): Reads the entire file into one single string. Use this only if the file is small.
  • f.readline(): Reads just one line from the file. Every time you call it, it moves to the next line.
  • f.readlines(): Reads the whole file and stores each line as an element in a list.

Reading Line by Line (Efficient)

For large files, it is best to use a for loop. This is memory-efficient because it only loads one line at a time.

with open("data.txt", "r") as f:
    for line in f:
        print(line.strip())

Note: We use .strip() because each line in a file usually ends with a hidden "newline" character \( \). strip() removes that extra space.


4. Writing to Text Files

To put data into a file, we use the .write() method. Note that .write() only accepts strings. If you want to save a number, you must convert it using str().

with open("output.txt", "w") as f:
    f.write("Score: " + str(100) + "\\n")

Key Takeaway: Remember to add your own newline character \( \) if you want the next piece of data to appear on a new line!


5. Serial vs. Sequential Files (2026 Syllabus Only)

If you are taking the exam in 2026, you need to know the difference between these two types of text files:

  • Serial Files: Data is stored in the order it arrives. There is no specific organization. Imagine a "To-Do" list where you just write tasks at the bottom as they come to mind. To find something, you must search from the very beginning (Linear Search).
  • Sequential Files: Data is stored in a specific order based on a key field (e.g., sorted by ID number). This makes it easier to process data in order, but adding new data is harder because you have to insert it into the correct spot.

6. Working with CSV Files

CSV stands for Comma-Separated Values. It is a common way to store table-like data (records and fields) in a text file. The 2027 syllabus Reference Guide specifically mentions the csv module.

Reading a CSV

import csv
with open("students.csv", "r") as f:
    reader = csv.reader(f)
    for row in reader:
        print(row) # Each row is a list of strings

Writing to a CSV

import csv
data = [["Name", "Grade"], ["Alice", "A"], ["Bob", "B"]]
with open("results.csv", "w", newline='') as f:
    writer = csv.writer(f)
    writer.writerows(data)

Memory Aid: Think of a CSV file as a "Flat File Database." It’s a simple table stored as text where commas represent the boundaries between columns.


7. Common Pitfalls to Avoid

  • File Not Found Error: This happens if you try to 'r' (read) a file that doesn't exist or if you misspelled the name. Always check if the file is in the same folder as your Python script.
  • Overwriting Data: Remember that 'w' wipes the file clean. If you want to keep existing data and just add more, use 'a' (append).
  • Data Types: Python will throw an error if you try to .write() an integer or a list directly. Always convert your data to a string first.
  • The Newline Trap: When reading a file, the \( \) at the end of a line counts as a character. Use .strip() or .rstrip() to clean it up.

Summary Checklist

By now, you should be comfortable with:

  • Explaining why we use files (Persistence).
  • Using open() with modes 'r', 'w', and 'a'.
  • Implementing the with statement for safe file handling.
  • Reading data using read(), readline(), or a for loop.
  • Writing data and handling newline characters.
  • (2027) Using the csv module to handle structured data.
  • (2026) Distinguishing between serial and sequential file organization.

Don't worry if this seems tricky at first! File handling is very logical—once you get the hang of the "Open-Process-Close" flow, it becomes second nature.