KwickAcademy Python · 7 min · free
Binary Files and the pickle Module
Learn binary files and pickle for CBSE Class 12: text against binary, dump and load, EOFError, and the search, append and update record programs. Pickling turns a Python object into bytes with pickle.dump(), and load() brings it back.
Follows the syllabus of: CBSE Class 12 Computer Science (083)
On screen in this lesson
Text file vs binary file
| Point | Text file | Binary file |
|---|---|---|
| Stores | characters | bytes |
| Open in Notepad | readable | looks like junk |
| Extension | .txt | .dat (usually) |
| Line endings | converted | never changed |
| Python data | strings only | any object |
Binary file modes
| Mode | Meaning | If file missing |
|---|---|---|
| rb | read | FileNotFoundError |
| wb | write, erases old | creates it |
| ab | add at the end | creates it |
| rb+ | read and write | FileNotFoundError |
| wb+ | write and read | creates it |
What does pickle do?
| Pickling: object to bytes, with pickle.dump() |
| Unpickling: bytes back to object, with pickle.load() |
| Also called serialization and deserialization |
| First write: import pickle |
How many records are there?
| load() reads one object per call |
| At the end of the file, load() raises EOFError |
| Catch it with try and except EOFError |
| Loop with while True until EOFError |
How do we update a record?
| Pickled records cannot be edited in place safely |
| Step 1: read all records into a list |
| Step 2: change the matching record in the list |
| Step 3: write all records back with wb |
Common mistakes
| Opening with w or r instead of wb or rb |
| Writing dump(f, rec) in the wrong order |
| Forgetting to catch EOFError |
| Using wb when you meant ab |
Quick answers
Why does load() raise EOFError?
Each call reads one object, and EOFError means there are no records left.
Why not update a record in place?
A changed record may need a different number of bytes, so you read all, change it and rewrite.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
Why does a .dat file look like junk in Notepad?44 sec
What is the argument order for dump?38 sec
Which mode adds a record without erasing?44 sec
Why use while True to read records?41 secThe full lesson, in text
Hello students, welcome to Kwickprep. A text file stores only strings. So how do we save a whole list, like a student record, and get it back as a list? The answer is a binary file and the pickle module. Today we learn dump, load, and the search, append and update programs that board papers ask.
Let us compare the two kinds of files. A text file stores characters, while a binary file stores raw bytes, the way memory holds data. Open a text file in Notepad and you can read it, but a binary file looks like junk. Text files usually end in dot txt, and our binary files will end in dot dat. In a text file, line endings may be converted, but a binary file is never changed. And a text file holds strings, while a binary file can hold any Python object.
Binary modes are the text modes with the letter B added. Read binary opens a file for reading, and a missing file gives FileNotFoundError. Write binary creates the file, but it also erases everything already inside. Append binary adds new data at the end and keeps the old data. Read binary plus lets you read and write an existing file. Write binary plus also reads and writes, but it erases the file first.
Pickle is a module that comes with Python, so there is nothing to install. Pickling means turning a Python object, like a list, into a stream of bytes, and dump does this. Unpickling means turning those bytes back into the same object, and load does this. Textbooks also call these serialization and deserialization. Before using either one, write import pickle at the top.
Each record here is a list of roll number, name and marks. With open, as f, opens the file and closes it by itself at the end of the block. Dump takes two things, the object and the file object, in that order. We dump two records into stu dot dat. Then we open it again in read binary mode. Each call to load reads exactly one record, back as a real list.
In a real file, we do not know how many records there are. Each call to load reads one object and moves ahead. When nothing is left, load raises an error called EOFError, which means end of file. We catch that error with try and except. So we loop with while True, and the except block ends the reading.
This function reads every record into a list called L. The file object, f, and the empty list L are created in one line. Inside try, the while True loop keeps loading and appending records. When the file ends, EOFError jumps to except, which closes the file. For our file, read all returns both records, Riya and Aman. We will reuse this function in the next programs.
To append means to add a record at the end without losing the old ones. So we open the file in append binary mode. Then we dump the new record. After calling add with Neha's record, the file holds three records. Pause and predict. What happens if you use write binary mode here by mistake? Riya and Aman are erased, and only Neha remains.
To search, we check each record, r, one by one. The roll number is at index zero of each record. Double equals compares it with the roll we want. On a match, we return that record at once. If the loop ends with no match, we return None. So search of two gives Aman's record, and search of nine gives None.
Updating is the trickiest program. A changed record may take a different number of bytes, so we should not overwrite it inside the file. Instead, first read all records into a list in memory. Next, change the matching record inside that list. Finally, open the file in write binary mode and dump every record back.
Here are the three steps in code. First, L holds all the records. The loop finds the record whose roll number matches, and index two, the marks, gets the new value. Then write binary mode rewrites the file, dumping each record again. After update of two comma eighty, Aman's marks change from seventy two to eighty.
A common exam question says, count the students who scored more than a cut off. Here cut is the cut off marks, and the variable, n, is the counter. For each record, index two holds the marks. If the marks are greater than cut, the counter goes up by one. After the update, the file has eighty eight, eighty and ninety five. So count above seventy five returns three.
Some questions store the whole class as one list, in a single dump. Then one load brings back the entire list, and no EOFError loop is needed. Here the list data holds two records. Load gives back the whole list, and len counts two records. Read the question carefully to see which style it uses.
Watch out for four common mistakes. Pickle needs binary modes, so plain read or write mode gives a TypeError. The order is dump of object comma file, never file first. Without catching EOFError, the reading loop crashes at the end of the file. And write binary instead of append binary erases every old record.
Let us revise what we learned today. Binary files store bytes, and pickle lets them store any Python object. Dump of object comma file writes, and each load reads one object. To read all records, loop until EOFError. Append in append binary mode, and update by reading all, changing, and rewriting in write binary mode. To search, compare one field of each record and return the match.
Courses that teach this
| Course | Unit |
|---|---|
| CBSE Class 12 Computer Science (083) | Computational Thinking and Programming – 2 |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

