You are on page 1of 4

TECHQueenUnacademy

Computer Science

Notes: Binary File. Grade:12th

Binary File:

A binary file is a file that contains data in a format that is not


human-readable. This type of file is typically used to store data that needs
to be accessed quickly and efficiently, such as images, audio files, and
video files.

Basic Operations on a Binary File:

The following are the basic operations that can be performed on a binary
file:

Open:

The file can be opened using the open() function. The open() function
takes two arguments: the name of the file and the mode in which the file
will be opened. The mode can be one of the following:

○ rb: Read-only mode. The file can only be read from.


○ wb: Write-only mode. The file can only be written to.
○ ab: Append mode. Data can be appended to the end of the
file.
○ rb+: Read-write mode. The file can be read from and written
to.

Close:

The file can be closed using the close() function. The close() function
flushes any buffered data to the file and releases any resources that are
associated with the file.

Import Pickle Module:

The pickle module can be used to serialize and deserialize objects.

Lovejeet Arora
TECHQueenUnacademy

Serialization is the process of converting an object into a stream of bytes.


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

Dump() and Load() Method:

The dump() method can be used to serialize an object to a file. The


load() method can be used to deserialize an object from a file.

Read, Write/Create, Search, Append and Update Operations in a


Binary File:

○ Read: The read() method can be used to read data from a


file. The read() method takes one argument: the number of
bytes to read.
○ Write/Create: The write() method can be used to write data
to a file. The write() method takes two arguments: the data
to be written and the number of bytes to write. If the file does
not exist, it will be created.
○ Search: The seek() method can be used to search for a
specific byte in a file. The seek() method takes two
arguments: the offset from the beginning of the file and the
direction of the search.
○ Append: The write() method can be used to append data to
the end of a file.
○ Update: The seek() and write() methods can be used to
update data in a file.

Examples

Lovejeet Arora
TECHQueenUnacademy

The following are some examples of how to perform the basic operations
on a binary file:

Python

# Open a file in read-only mode


file = open("myfile.txt", "rb")

# Read data from the file


data = file.read()

# Close the file


file.close()

# Open a file in write-only mode


file = open("myfile.txt", "wb")

# Write data to the file


file.write("This is some data")

# Close the file


file.close()

# Open a file in append mode


file = open("myfile.txt", "ab")

# Append data to the file


file.write("This is some more data")

# Close the file


file.close()

# Import the pickle module


import pickle

# Create an object
my_object = {"name": "John Doe", "age": 30}

# Serialize the object


pickle.dump(my_object, open("myobject.pkl", "wb"))

Lovejeet Arora
TECHQueenUnacademy

# Deserialize the object


my_object_deserialized = pickle.load(open("myobject.pkl",
"rb"))

# Print the object


print(my_object_deserialized)

Lovejeet Arora

You might also like