0% found this document useful (0 votes)
2 views4 pages

Class12 Binary Files Python Guide

This guide covers binary files in Python, detailing their definition, advantages, and disadvantages, as well as the use of the pickle module for reading and writing data. It includes examples of writing and reading single and multiple records, updating records, and common errors encountered. Additionally, it provides important exam tips and practice programs to enhance understanding of binary file handling.

Uploaded by

Gouri Padma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Class12 Binary Files Python Guide

This guide covers binary files in Python, detailing their definition, advantages, and disadvantages, as well as the use of the pickle module for reading and writing data. It includes examples of writing and reading single and multiple records, updating records, and common errors encountered. Additionally, it provides important exam tips and practice programs to enhance understanding of binary file handling.

Uploaded by

Gouri Padma
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Class 12 Python Binary Files – Complete Guide

This guide explains binary files in Python according to the Class 12 Computer Science syllabus. It includes concepts,
modes, pickle module, updating records, important points, examples, common mistakes, and exam tips.

1. What is a Binary File?


A binary file stores data in binary format (0s and 1s). Humans cannot directly read it.
Python commonly uses the pickle module to store Python objects in binary files.

Examples of binary files:


• Images
• Videos
• Audio files
• Pickle files

2. Why Use Binary Files?


Advantages:
• Faster than text files
• Can store Python objects directly
• More secure and compact
• Useful for records like student details

Disadvantages:
• Cannot be read normally by humans
• Requires pickle module

3. Importing Pickle Module


Before working with binary files:

import pickle

4. File Modes Used with Binary Files


rb → Read binary
wb → Write binary
ab → Append binary
rb+ → Read and write binary

Important:
Binary modes always contain 'b'.

5. Writing into a Binary File


Example:

import pickle

f = open("[Link]", "wb")

data = ["Anu", 95, "Kochi"]

[Link](data, f)

[Link]()

Explanation:
[Link]() writes Python objects into the binary file.

6. Reading from a Binary File


Example:

import pickle

f = open("[Link]", "rb")

d = [Link](f)

print(d)

[Link]()

Output:
['Anu', 95, 'Kochi']

7. Writing Multiple Records


Example:

import pickle

f = open("[Link]", "wb")

l = [
["Anu", 95, "Kochi"],
["Rahul", 88, "Delhi"]
]

[Link](l, f)

[Link]()

8. Reading Multiple Records Using EOFError


Example:

import pickle

f = open("[Link]", "rb")

try:
while True:
d = [Link](f)
print(d)

except EOFError:
[Link]()

Why EOFError?
Because Python must know when file data ends.

9. Appending Records
Example:

import pickle

f = open("[Link]", "ab")

rec = ["Maya", 90, "Mumbai"]

[Link](rec, f)
[Link]()

'ab' mode adds data at the end.

10. Updating Records


Important concept:
You usually read all records into a list, modify them,
then rewrite the file.

Example:

import pickle

f = open("[Link]", "rb+")

d = [Link](f)

for i in d:
if i[0] == "Anu":
i[1] = 99

[Link](0)

[Link](d, f)

[Link]()

Why use seek(0)?


After reading, the file pointer reaches the end.
seek(0) moves it back to the beginning.

11. File Pointer Concept


The file pointer acts like a cursor.

Before reading:
^ data

After reading:
data ^

After seek(0):
^ data

Without seek(0), updated data gets written at the end instead of replacing old data.

12. Difference Between Modes


r+ → text read/write
rb+ → binary read/write

w → text write
wb → binary write

a → text append
ab → binary append

13. Common Errors


1. FileNotFoundError
Occurs when file does not exist.

2. EOFError
Occurs when reading beyond file end.
3. UnicodeDecodeError
Occurs if binary file is opened in text mode.

4. TypeError
Occurs if wrong datatype is used.

14. Important Exam Points


• Always import pickle for binary files.
• Use dump() for writing.
• Use load() for reading.
• Binary files must use 'b' mode.
• Use seek(0) before rewriting updated data.
• Close files after use.
• EOFError is commonly used while reading multiple records.

15. Frequently Asked Questions


Q1. Why is binary file faster?
Because data is stored directly in machine-readable format.

Q2. Why use pickle?


It converts Python objects into binary format.

Q3. Why use rb+?


To read and write without deleting file contents.

Q4. What does seek(0) do?


Moves file pointer to the beginning.

16. Practice Programs


1. Write student records into binary file.
2. Read records from binary file.
3. Search a student by name.
4. Append new record.
5. Update marks.
6. Delete a record.

End of Guide — Practice writing small programs regularly to master binary files.

You might also like