Explore Binary Files in Python Class 12 in easy manner

In this post, we will discuss an introduction to binary files in python class 12. Before starting with this concept you need to understand that binary files are important for file handling in python. So we will cover the following topic in this post:

Understanding of Binary files in Python Class 12

The binary file refers to a file which is not readable by users directly. It is stored in a numeric pattern or follow some binary codes or a file which is processed quick then text files. Unlike text files binary doesn’t have any end line character or any character which is going to be translated into machine-readable form. The files like images, audio files or video files are examples of binary files. So let’s start with Binary File operations in Python. So let us start binary file operations.
 
The next topic of Binary Files in Python Class 12 is open a file to read and write in with binary mode.

Open a file (FileName – Absolute and relative path, mode)

When you are working with computers everything finally saved into files. To save these files we are using folders or directories. Folders can have subfolders. The complete address of a file is called a path.
 
For example: D:\School Work\School Work 2020-21\Website Contents\CS XIIbinary.docx.
 
This example is known as the absolute path as it contains all folder names from top to bottom. 
windows directory structure
windows directory structure
 
In windows, you can access files and folders using file explorer. When you are using MS Word or Excel or any other software for opening or saving file, the file dialog box opens a recently used folder or documents folder by default. This folder is marked as Current Working Directory (CWD) in the Operating System. This path is known as a relative path. 
The default relative path of python is:C:\Users\utcl\AppData\Local\Programs\Python\Python38-32
 
There are three basic modes of a binary file:
  • read: This mode is written as rb
  • write: This mode is written as wb
  • append: This mode is written as ab

The plus symbol followed by file mode is used to perform multiple operations together. For example, r+ is used for reading the opening file for reading and writing. The cursor position is at the beginning when + symbol is written with file mode. 

 
To open a binary file follow this syntax:
file = open(<filepath>, mode)
For example: f = open(“one.dat”,”rb”) 

Close File

When you are leaving the program or at the end of your file operation in the program file should be closed to avoid data corruption or any garbage data generation. The syntax of closing a binary file is similar to a text file.
 
file.close()

The next topic is a very important factor for Binary Files in Python Class 12. This will help to read and write in python. 

Click here to read Binary File Practicals

Pickle Module – dump() and load() functions

When you are working with files in computers either you need to understand or convert the files from one mode to another.
 
Let’s understand in a simple way. If a person who is speaking in Spanish language and a person who is speaking in the Hindi language in communication then it has meaning at all. In this process, we need one person who knows both languages.
 
Similarly in computers also pickle module is doing something exactly (conversion of python objects) in binary. This process is known as pickling.
It is also known as serialization or flattening or marshalling. 
The reverse process is known as unpickling. To do this process you need to import pickle module.

dump() function

The dump() function or method is usually used with write to object or we can say encoding in python when the file is accessed in binary mode. 
 
This dump() function accepts two parameters.
  • Python Object which needs to be encoded or write 
  • Data File object in which values of the python object should be written

load() function

The load() function or method is used to reading data from a binary file or we can say decoding in python when the file is accessed in binary mode. A variable should be used to assign the values from the file and decoded. 

Now you will learn hor to write contents into binary file for Binary Files in Python Class 12. 

Write into a binary file

To write into binary file follow these steps:
  • Initiate a python object such as list, dictionary or any other object
  • create a file using the open function
  • use pickle.dump() method with parameters python object and file object

In the next section of Binary Files in Python Class 12, you will learn how to read data from a binary file.

read from binary file

To read from binary file follow these steps:
  • Open file using open() function
  • instantiate an object to store data read from a file using load() function
  • Print the data
Look at this example for binary files in python class 12:
import pickle
def bin_write():
d = {'Windows 95':1995,'Windows 98':1998,'Windows ME':2000,'Windows XP':2001,
'Windows 95':1995,'Windows 7':2009,'Windows 8':2012,'Windows 10':2015}
f = open('pcm_result.dat','wb')
pickle.dump(d,f)
f.close()
f = open('pcm_result.dat','rb')
d1 = pickle.load(f)
f.close()
for n,y in d1.items():
print(n,"-->", y)
bin_write()
Output

Output read and write in binary file
Output read and write in binary file

In the above example, one dictionary object is created and instantiated with d and initialized with values like windows versions and their launching years.

The binary file is opened using “wb” mode with a relative path. Then the data are written into a binary file with pickle.dump(d,f) method.

A file closed with f.close() and then reopened for reading purpose with “rb” mode with a relative path. Then data read through a pickle.load(f) into d1 object.

Finally, data printed on the screen with for loop.

In this article Binary Files in Python Class 12 you learned the basics of binary files in python class 12. 

Thank you for reading the article Binary Files in Python Class 12. I would like to hear from your side regarding this post. 

Computer Science Class 12

Recommended – Assignment Binary File

3 thoughts on “Explore Binary Files in Python Class 12 in easy manner”

  1. very good notes please guide how to download

  2. If you are using mobile use share buttons coming at bottom and tap on pdmyurl button at the last. If you are using PC/Laptop it is coming at right side floating share buttons screen, click on pdfmyrul button. Button no. 5 in the floating share buttons. If till you have any problem, feel free to comment or write us using contact us button.

  3. Pingback: Class 12 Important Questions - File Handling - Computer Science with Python - TutorialAICSIP

Comment Your Views

%d bloggers like this: