CBSE Class 12 CS (083) - File Handling: Binary and CSV

File Handling - Binary and CSV Files

Binary files store data in binary format (0s and 1s) and are used for non-text data. CSV (Comma Separated Values) files store tabular data in plain text. This chapter covers reading and writing binary files using the pickle module and CSV files using the csv module.

1. Introduction to Binary Files

A binary file stores data in the same format as it is stored in memory. Unlike text files, binary files are not human-readable. They store data as a sequence of bytes.

Characteristics:

  • Data is stored in binary format (0s and 1s).
  • Not human-readable without special tools.
  • More efficient for storing complex data structures.
  • Faster to read and write compared to text files.
  • No newline character conversion.

Common uses: Images (.jpg, .png), audio (.mp3), video (.mp4), executable files (.exe), and Python object serialization (.dat, .pkl).

2. Binary File Modes

To work with binary files, append "b" to the file mode:

ModeDescription
"rb"Read binary
"wb"Write binary (overwrites)
"ab"Append binary
"rb+"Read and write binary
"wb+"Write and read binary
"ab+"Append and read binary

3. The Pickle Module

The pickle module in Python is used for serializing and deserializing Python objects. Serialization converts a Python object into a byte stream, which can be stored in a binary file.

import pickle

Key functions:

FunctionDescription
pickle.dump(obj, file)Serializes obj and writes it to the binary file
pickle.load(file)Reads and deserializes the next object from the binary file

4. Writing with dump()

import pickle

# Storing a list in a binary file
students = ["Riya", "Amit", "Priya", "Rahul"]

with open("students.dat", "wb") as f:
pickle.dump(students, f)

print("Data written successfully.")

You can store multiple objects by calling dump() multiple times.

with open("data.dat", "wb") as f:
pickle.dump(100, f) # Integer
pickle.dump(3.14, f) # Float
pickle.dump("Hello", f) # String
pickle.dump([1, 2, 3], f) # List

5. Reading with load()

import pickle

with open("students.dat", "rb") as f:
data = pickle.load(f)
print(data) # Output: ["Riya", "Amit", "Priya", "Rahul"]

When multiple objects are stored, call load() in the same order as dump() was called. An EOFError is raised when the end of file is reached.

with open("data.dat", "rb") as f:
print(pickle.load(f)) # 100
print(pickle.load(f)) # 3.14
print(pickle.load(f)) # Hello
print(pickle.load(f)) # [1, 2, 3]

6. Binary File Example Program

import pickle

def add_student():
students = []
n = int(input("Enter number of students: "))
for i in range(n):
roll = int(input("Enter roll number: "))
name = input("Enter name: ")
marks = float(input("Enter marks: "))
students.append({"roll": roll, "name": name, "marks": marks})
with open("students.dat", "wb") as f:
pickle.dump(students, f)

def display_students():
try:
with open("students.dat", "rb") as f:
data = pickle.load(f)
for s in data:
print(f"Roll: {s['roll']}, Name: {s['name']}, Marks: {s['marks']}")
except FileNotFoundError:
print("File not found.")

7. Searching and Updating in Binary Files

def search_student(roll_no):
try:
with open("students.dat", "rb") as f:
data = pickle.load(f)
for s in data:
if s["roll"] == roll_no:
print(f"Found: {s['name']}, Marks: {s['marks']}")
return
print("Student not found.")
except FileNotFoundError:
print("File not found.")

Updating: To update a record, read all data, modify the required record, and write back the entire data.

8. Introduction to CSV Files

A CSV (Comma Separated Values) file stores tabular data in plain text. Each line represents a row, and values are separated by commas.

Roll,Name,Marks
101,Riya,95
102,Amit,87
103,Priya,92

CSV files can be opened in any text editor or spreadsheet application like Excel.

9. The CSV Module

Python provides the csv module for working with CSV files.

import csv

Key functions:

Function/ClassDescription
csv.reader(file)Returns a reader object that iterates over rows
csv.writer(file)Returns a writer object for writing rows
csv.DictReader(file)Reads CSV as dictionaries (header as keys)
csv.DictWriter(file)Writes CSV from dictionaries

10. Reading CSV Files with reader()

import csv

with open("students.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
print(row)

To skip the header row:

with open("students.csv", "r") as f:
reader = csv.reader(f)
next(reader) # Skip header
for row in reader:
print(f"Roll: {row[0]}, Name: {row[1]}, Marks: {row[2]}")

11. Writing CSV Files with writer()

import csv

data = [
["Roll", "Name", "Marks"],
[101, "Riya", 95],
[102, "Amit", 87],
[103, "Priya", 92]
]

with open("students.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(data)

The newline="" parameter prevents extra blank rows on Windows.

12. Using DictReader

import csv

with open("students.csv", "r") as f:
reader = csv.DictReader(f)
for row in reader:
print(row["Name"], row["Marks"])

DictReader uses the first row as header to create dictionary keys.

13. Using DictWriter

import csv

students = [
{"Roll": 101, "Name": "Riya", "Marks": 95},
{"Roll": 102, "Name": "Amit", "Marks": 87}
]

with open("students.csv", "w", newline="") as f:
fieldnames = ["Roll", "Name", "Marks"]
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(students)

14. CSV File Example Program

import csv

def add_record():
with open("records.csv", "a", newline="") as f:
writer = csv.writer(f)
roll = int(input("Enter roll: "))
name = input("Enter name: ")
marks = float(input("Enter marks: "))
writer.writerow([roll, name, marks])

def display_all():
try:
with open("records.csv", "r") as f:
reader = csv.reader(f)
for row in reader:
print(", ".join(row))
except FileNotFoundError:
print("File not found.")

15. Text vs Binary vs CSV - Comparison

FeatureText FileBinary FileCSV File
FormatHuman-readableMachine-readableHuman-readable
Extension.txt.dat, .bin, .pkl.csv
Data TypesStrings onlyAny Python objectStrings (numeric stored as text)
File SizeLargerSmallerModerate
Processing SpeedSlowerFasterModerate
Module UsedBuilt-in open()picklecsv

16. Revision Questions and Answers

Very Short Answer Questions

1. Which module is used for binary file handling in Python?
The pickle module.

2. What does pickle.dump() do?
It serializes a Python object and writes it to a binary file.

3. Which function reads an object from a binary file?
pickle.load().

4. What file mode is used to write to a binary file?
"wb".

5. Which module handles CSV files in Python?
The csv module.

Short Answer Questions

1. Differentiate between dump() and load().
pickle.dump(obj, file) writes a serialized Python object to a binary file. pickle.load(file) reads and deserializes the next object from a binary file. They are complementary operations.

2. How do you read a CSV file using DictReader?
DictReader reads the first row as header and returns each subsequent row as a dictionary where keys are column names. Example: with open("file.csv") as f: reader = csv.DictReader(f); for row in reader: print(row["Name"]).

3. What is the purpose of newline="" when writing CSV files?
It prevents extra blank rows from being added between data rows on Windows systems due to how line endings are handled.

Long Answer Questions

1. Write a Python program to create a binary file "employee.dat" containing employee records (emp_id, name, salary). Then read and display all records.

import pickle
def create_employee():
employees = []
n = int(input("Enter number of employees: "))
for i in range(n):
emp_id = int(input("Enter employee ID: "))
name = input("Enter name: ")
salary = float(input("Enter salary: "))
employees.append({"id": emp_id, "name": name, "salary": salary})
with open("employee.dat", "wb") as f:
pickle.dump(employees, f)

def display_employees():
try:
with open("employee.dat", "rb") as f:
data = pickle.load(f)
for emp in data:
print(f"ID: {emp['id']}, Name: {emp['name']}, Salary: {emp['salary']}")
except FileNotFoundError:
print("File not found.")

2. Explain how to write tabular data from a list of dictionaries to a CSV file.
Use DictWriter with fieldnames matching the dictionary keys. Call writeheader() to write column names, then writerows() to write all data. Example: writer = csv.DictWriter(f, fieldnames=["Name", "Age"]); writer.writeheader(); writer.writerows(data).