Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Chapter 4: File Handling in Python

So far, the programs we have written store their data in variables in the computer’s primary memory (RAM). However, data in RAM is volatile—it disappears when the program ends or the computer shuts down.

To store data permanently, we must save it to a file on a secondary storage device (like a hard drive or SSD). The process of working with files (reading, writing, and manipulating them) is called File Handling.

4.1 Types of Files

Python primarily deals with two main types of files:

  1. Text Files:
    • Store data in ASCII or Unicode formats (plain text).
    • Human-readable.
    • Each line ends with an End-of-Line (EOL) character (usually \n in Python).
    • Examples: .txt, .py, .html, .xml.
  2. Binary Files:
    • Store data in the exact same format as it is held in memory (0s and 1s).
    • Not human-readable (looks like gibberish if opened in a text editor).
    • Faster and more efficient for storing complex data structures (like objects, images, audio, etc.).
    • Examples: .dat, .jpg, .mp3.

We will also cover CSV (Comma Separated Values) files, which are a specific type of text file used to store tabular data.


4.2 Absolute and Relative Paths

To access a file, the Python interpreter needs to know where it is located natively on your computer’s filesystem.

  • Absolute Path: The complete path from the root directory to the file (e.g., C:\Users\Student\Documents\data.txt or /home/user/data.txt).
  • Relative Path: The path to the file relative to the current working directory (the folder where your Python script is running). If script.py and data.txt are in the same folder, the relative path is just data.txt.

4.3 Text File Operations

4.3.1 Opening and Closing a Text File

You must open a file before reading or writing to it. The open() function returns a file object which you use to interact with the file.

# Syntax: file_object = open(file_name, access_mode)

Common Text File Open Modes:

  • 'r': Read (default). Opens for reading. Errors if the file doesn’t exist.
  • 'w': Write. Opens for writing. Creates a new file or overwrites an existing file completely.
  • 'a': Append. Opens for appending. Adds new data to the end of the file. Creates a new file if it doesn’t exist.
  • 'r+': Read & Write. Opens for reading and writing. The file pointer is at the beginning.
  • 'w+': Write & Read. Opens for writing and reading. Overwrites if exists, creates if not.
  • 'a+': Append & Read. Opens for appending and reading. The file pointer is at the end.

Always remember to close a file after you are done! This saves changes and frees up system resources.

file_obj = open("story.txt", "r") 
# ... perform operations ...
file_obj.close() 

Using the with statement (Recommended Approach): The with statement automatically manages closing the file, even if exceptions occur. You don’t need a close() statement!

with open("story.txt", "w") as f:
    f.write("Chapter 1: The Beginning")
# 'f' is automatically closed here

4.3.2 Writing to a Text File

  • write(string): Writes a specified string to the file.
  • writelines(list_of_strings): Writes a list of strings to the file. (You must add \n manually if you want them on separate lines).
lines = ["Line 1\n", "Line 2\n", "Line 3\n"]

with open("output.txt", "w") as file:
    file.write("This is a single line.\n")
    file.writelines(lines)

4.3.3 Reading from a Text File

  • read([size]): Reads and returns the entire file contents as a single string. If size is specified, it reads at most size bytes (characters).
  • readline(): Reads and returns exactly one line, including the newline character \n.
  • readlines(): Reads all lines and returns them as a list of strings, where each string is a line.
with open("output.txt", "r") as file:
    content = file.read()
    print("Full Content:\n", content)

with open("output.txt", "r") as file:
    line1 = file.readline()
    print("First Line:", line1)

with open("output.txt", "r") as file:
    line_list = file.readlines()
    print("List of Lines:", line_list)

4.3.4 The seek() and tell() Methods

Python maintains a file pointer (like a cursor) that indicates the current position within the file.

  • file.tell(): Returns the current byte location of the file pointer.
  • file.seek(offset, from_what): Moves the file pointer to a new location.
    • offset: Number of bytes to move.
    • from_what: Reference point:
      • 0: Beginning of the file (default).
      • 1: Current position (only works in binary mode!).
      • 2: End of the file (only works in binary mode!).

4.4 Binary File Operations

Binary files store data exactly as it is represented in memory. To work with complex Python objects (like dictionaries or lists) in binary files, we use the pickle module. The process of converting objects to a byte stream is called pickling (or serialization), and converting them back is unpickling (deserialization).

File modes for binary: rb, wb, ab, rb+, wb+, ab+. (Notice the b appended to the text modes).

4.4.1 Pickling (Writing to a Binary File)

Use the pickle.dump(object, file_object) method.

import pickle

student_record = {"Roll": 1, "Name": "Amit", "Marks": 85}

with open("student.dat", "wb") as f:
    pickle.dump(student_record, f) # Serializes dictionary to binary file

4.4.2 Unpickling (Reading from a Binary File)

Use the pickle.load(file_object) method. It reconstructs the object.

import pickle

with open("student.dat", "rb") as f:
    # Deserializes the binary data back into a dictionary
    retrieved_data = pickle.load(f) 
    print(retrieved_data['Name']) # 'Amit'

4.5 CSV File Operations

CSV (Comma Separated Values) files are a simple, text-based format for storing tabular data (like spreadsheets or databases). Each line is a row, and values are separated by commas.

Python provides the built-in csv module to handle these files easily.

4.5.1 Writing to a CSV File

  • csv.writer(file_object): Creates a writer object.
  • writerow(list): Writes a single row.
  • writerows(list_of_lists): Writes multiple rows.
import csv

data = [
    ["ID", "Name", "Department"],
    [101, "Aarav", "IT"],
    [102, "Kavya", "HR"]
]

# We use newline='' to prevent blank lines between rows in Windows
with open("employees.csv", "w", newline='') as file:
    csv_writer = csv.writer(file)
    csv_writer.writerows(data)

4.5.2 Reading from a CSV File

  • csv.reader(file_object): Creates a reader object that you can iterate over.
import csv

with open("employees.csv", "r") as file:
    csv_reader = csv.reader(file)
    for row in csv_reader:
        print(row) # Each row is returned as a list of strings

Mastering file handling is crucial because almost every real-world application requires saving and loading persistent data!