[Computer Science Class 12] Binary File - File Handling in Python (2024)

A binary file is a file that contains data in the form of bytes , which can represent any type of data, such as images, audio, video, executable code, etc. A binary file cannot be read or edited using a text editor , but requires a specific program or application that can understand its format.

Opening a binary file

To open binary files in Python, we need to use the “b” character in the mode argument of the open() function.

The file open modes for binary files are similar to the text file open modes, except that they use the “b” character to indicate binary mode. The meaning of each mode is as follows:

Mode

Description

“rb”

Open a binary file for reading only. The file pointer is placed at the beginning of the file.

“rb+”

Open a binary file for both reading and writing. The file pointer is placed at the beginning of the file.

“wb”

Open a binary file for writing only. Overwrites the file if it exists. Creates a new file if it does not exist.

“wb+”

Open a binary file for both writing and reading. Overwrites the existing file if it exists. Creates a new file if it does not exist.

“ab”

Open a binary file for appending. The file pointer is at the end of the file if it exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

“ab+”

Open a binary file for both appending and reading. The file pointer is at the end of the file if it exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

Closing a binary file

This is done using the close() method of the file object , just like in text files. The close() method releases the resources associated with the file and ensures that any changes made to the file are saved. It is important to close a file after using it to avoid errors or data loss.

For example, to close the file object f, we can write:

f.close()

Pickle Module

The pickle module is a built-in module that provides functions for serializing and deserializing Python objects.

Serialization is the process of converting an object into a stream of bytes that can be stored in a file or transmitted over a network.

Deserialization is the reverse process of converting a stream of bytes back into an object .

The pickle module can handle most Python objects, such as lists, dictionaries, classes, functions, etc., but not all.

To use the pickle module, we need to import it first:

import pickle

The pickle module provides two methods - dump() and load() to work with binary files for pickling and unpickling, respectively.

The dump( ) method

The dump() method takes an object and a file object as arguments and writes the serialized bytes of the object to the file.

The file in which data are to be dumped, needs to be opened in binary write mode (wb) .

Syntax of dump() is as follows:

dump(data_object, file_object)

where data_object is the object that has to be dumped to the file with the file handle named file_object.

For example, the program given below writes the record of a student (roll_no, name, gender and marks) in the binary file named mybinary.dat using the dump(). We need to close the file after pickling:

import pickle

listvalues=[1,"Geetika",'F', 26]

fileobject=open("mybinary.dat", "wb")

pickle.dump(listvalues,fileobject)

fileobject.close()

The load( ) method

The load() method takes a file object as an argument and returns the deserialized object from the bytes read from the file.

The file to be loaded is opened in binary read (rb) mode.

Syntax of load() is as follows:

Store_object = load(file_object)

Here, the pickled Python object is loaded from the file having a file handle named file_object and is stored in a new file handle called store_object.

For example, the program given below demonstrates how to read data from the file mybinary.dat using the load() method:

import pickle print("The data that were stored in file are: ")

fileobject=open("mybinary.dat","rb")

objectvar=pickle.load(fileobject)

fileobject.close()

print(objectvar)

Read, write/create, search, append and update operations in a binary file

These are some common operations that can be performed on a binary file using different methods and functions . Some examples are:

1. read

To read data from a binary file, we can use methods like read(), readline(), or readlines() , just like in text files. However, these methods will return bytes objects instead of strings. We can also use struct.unpack() to convert bytes into other data types, such as integers, floats, etc.

For example, to read an integer from a binary file, we can write:

import struct

# open a binary file in read mode

f = open("number.bin", "rb")

# read 4 bytes from the file

data = f.read(4)

# unpack the bytes into an integer

number = struct.unpack("i", data)[0]

# close the file

f.close()

# print the number

print(number)

2. write/create

To write or create data in a binary file, we can use methods like write() or writelines() , just like in text files. However, these methods will take bytes objects instead of strings. We can also use struct.pack() to convert other data types into bytes, such as integers, floats, etc.

For example, to write an integer to a binary file, we can write:

import struct

# open a binary file in write mode

f = open("number.bin", "wb")

# pack an integer into 4 bytes

data = struct.pack("i", 42)

# write the bytes to the file

f.write(data)

# close the file

f.close()

3. search

To search for a specific data in a binary file, we can use a loop to iterate over the bytes or records in the file and compare them with the target data. We can also use methods like tell() and seek() to get or set the position of the file pointer .

For example, to search for an integer in a binary file, we can write:

import struct

# open a binary file in read mode

f = open("numbers.bin", "rb")

# define the target integer to search for

target = 42

# define a flag to indicate whether the target is found or not

found = False

# loop until the end of the file is reached

while True:

# read 4 bytes from the file

data = f.read(4)

# if no more data is available, break the loop

if not data:

break

# unpack the bytes into an integer

number = struct.unpack("i", data)[0]

# compare the number with the target

if number == target:

# get the current position of the file pointer

pos = f.tell()

# print a message with the position of the target

print(f"Found {target} at position {pos - 4}")

# set the flag to True

found = True

# break the loop (optional)

break

# close the file

f.close()

# if the flag is still False, print a message that the target is not found

if not found:

print(f"{target} is not found in the file")

4. append

To append data to a binary file, we can use methods like write() or writelines() , just like in text files. However, we need to open the file in append mode (“ab” or “ab+”) instead of write mode (“wb” or “wb+”).

For example, to append an integer to a binary file, we can write:

import struct

# open a binary file in append mode

f = open("numbers.bin", "ab")

# pack an integer into 4 bytes

data = struct.pack("i", 42)

# write the bytes to the end of the file

f.write(data)

# close the file

f.close()

5. update

To update data in a binary file, we can use methods like write() or writelines(), just like in text files. However, we need to open the file in read and write mode (“rb+” or “wb+”) instead of read mode (“rb”) or write mode (“wb”). We also need to use methods like tell() and seek() to get or set the position of the file pointer .

For example, to update an integer in a binary file, we can write:

import struct

# open a binary file in read and write mode

f = open("numbers.bin", "rb+")

# define the target integer to update and its new value

target = 42

new_value = 99

# loop until the end of the file is reached

while True:

# read 4 bytes from the file

data = f.read(4)

# if no more data is available, break the loop

if not data:

break

# unpack the bytes into an integer

number = struct.unpack("i", data)[0]

# compare the number with the target

if number == target:

# get the current position of the file pointer

pos = f.tell()

# move the file pointer back by 4 bytes

f.seek(pos - 4)

# pack the new value into 4 bytes

data = struct.pack("i", new_value)

# write the bytes to the file, overwriting the old value

f.write(data)

# print a message that the target is updated

print(f"Updated {target} to {new_value} at position {pos - 4}")

# break the loop (optional)

break

# close the file

f.close()

[Computer Science Class 12] Binary File - File Handling in Python (2024)
Top Articles
Does S23 Series have SIM card slot or does it support e-SIM?
Arbitrum address example | Coin Wallet
WALB Locker Room Report Week 5 2024
San Angelo, Texas: eine Oase für Kunstliebhaber
Jail Inquiry | Polk County Sheriff's Office
Somboun Asian Market
Coindraw App
Blairsville Online Yard Sale
Plus Portals Stscg
Calamity Hallowed Ore
T&G Pallet Liquidation
Deshret's Spirit
Autozone Locations Near Me
Encore Atlanta Cheer Competition
The Rise of Breckie Hill: How She Became a Social Media Star | Entertainment
10 Free Employee Handbook Templates in Word & ClickUp
Char-Em Isd
Rachel Griffin Bikini
Sni 35 Wiring Diagram
Sea To Dallas Google Flights
Boscov's Bus Trips
Sunset Time November 5 2022
Rust Belt Revival Auctions
European city that's best to visit from the UK by train has amazing beer
Sister Souljah Net Worth
Sherburne Refuge Bulldogs
Bento - A link in bio, but rich and beautiful.
TJ Maxx‘s Top 12 Competitors: An Expert Analysis - Marketing Scoop
My Reading Manga Gay
Guide to Cost-Benefit Analysis of Investment Projects Economic appraisal tool for Cohesion Policy 2014-2020
ATM, 3813 N Woodlawn Blvd, Wichita, KS 67220, US - MapQuest
Missing 2023 Showtimes Near Mjr Southgate
Shiftwizard Login Johnston
Here’s how you can get a foot detox at home!
Metro By T Mobile Sign In
A Man Called Otto Showtimes Near Amc Muncie 12
Build-A-Team: Putting together the best Cathedral basketball team
Wsbtv Fish And Game Report
Merkantilismus – Staatslexikon
Überblick zum Barotrauma - Überblick zum Barotrauma - MSD Manual Profi-Ausgabe
WorldAccount | Data Protection
Yogu Cheshire
Acts 16 Nkjv
Coffee County Tag Office Douglas Ga
National Weather Service Richmond Va
Grand Valley State University Library Hours
Yale College Confidential 2027
Rheumatoid Arthritis Statpearls
Craigslist Marshfield Mo
Suzanne Olsen Swift River
Karen Kripas Obituary
Bloons Tower Defense 1 Unblocked
Latest Posts
Article information

Author: Arielle Torp

Last Updated:

Views: 5696

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Arielle Torp

Birthday: 1997-09-20

Address: 87313 Erdman Vista, North Dustinborough, WA 37563

Phone: +97216742823598

Job: Central Technology Officer

Hobby: Taekwondo, Macrame, Foreign language learning, Kite flying, Cooking, Skiing, Computer programming

Introduction: My name is Arielle Torp, I am a comfortable, kind, zealous, lovely, jolly, colorful, adventurous person who loves writing and wants to share my knowledge and understanding with you.