Dremendo Tag Line

Binary File Handling in Python

File Handling in Python

In this lesson, we will understand what Binary File is and how to create and handle them in Python Programming, along with some examples.

What is Binary File Handling

Binary File Handling is a process in which we create a file and store data in its original format. It means that if we store an integer value in a binary file, the value will be treated as an integer rather than text.

Binary files are mainly used for storing records, just as we store records in a database. It makes it easier to access or modify the records easily.

video-poster

Binary File Opening Modes

Below we have discussed the different types of binary file opening modes used to open a binary file in python.

Mode Description
rb Open file in binary mode for reading only. The file pointer stands at the beginning of the file.
rb+ Open file in binary mode for both reading and writing. The file pointer stands at the beginning of the file.
wb Open file in binary mode for writing only. It creates the file if it does not exist. If the file exists, then it erases all the contents of the file. The file pointer stands at the beginning of the file.
wb+ Open file in binary mode for both reading and writing. It creates the file if it does not exist. If the file exists, then it erases all the contents of the file. The file pointer stands at the beginning of the file.
ab Open file in binary mode for appending data. Data is added to the end of the file. It creates the file if it does not exist. The file pointer stands at the end of the file.
ab+ Open a file in binary mode for reading and appending data. Data is added to the end of the file. It creates the file if it does not exist. The file pointer stands at the end of the file.

Serialization or Pickling

Serialization or Pickling is the process in which python objects are converted into byte streams (sequence of bytes) so that they can be stored in a binary file or transferred over a network.

Deserialization or Unpickling

Deserialization or Unpickling is the process in which byte streams are converted back into python objects. Deserialization produces a replica of the original object.

The pickle Module

The pickle module provides methods for serialization and deserialization of python objects. There are four methods available in the pickle module, and they are:

  • dump()
  • load()
  • dumps()
  • loads()

dump() Method

The dump() method converts the python objects into byte streams and writes them immediately into a binary file.

Syntax of dump() method

pickle.dump(obj, file)

Example

import pickle

# store roll and name of 5 students in a binary file record.txt
with open(r'D:\record.txt', 'ab+') as fileobj:

    print('Input roll and name of 5 students\n')
    for i in range(5):
        roll = int(input('Roll: '))
        name = input('Name: ')
        data = [roll, name]

        # write the object data into binary file using the dump() method
        pickle.dump(data, fileobj)

print('All data stored successfully')

Output

Input roll and name of 5 students

Roll: 1
Name: Allen
Roll: 2
Name: Thomas
Roll: 3
Name: David
Roll: 4
Name: William
Roll: 5
Name: Henry
All data stored successfully

load() Method

The load() method reads the byte stream of one or more python objects from a binary file and reconstructs the replica of the original python object.

Syntax of load() method

obj = pickle.load(file)

Example

import pickle

# read all the roll and name of 5 students from the binary file record.txt
with open(r'D:\record.txt', 'rb') as fileobj:

    while True:
        try:
            # load individual student list into data using the load() method
            data = pickle.load(fileobj)
            print('Roll: %d' %(data[0]))
            print('Name: %s' %(data[1]))

        except EOFError:
            break

Output

Roll: 1
Name: Allen
Roll: 2
Name: Thomas
Roll: 3
Name: David
Roll: 4
Name: William
Roll: 5
Name: Henry

dumps() Method

The dumps() method only converts the python objects into a string of bytes and return them as byte object. This method itself does not write the byte object into a binary file. We must use the write() method of the file object to write it into a binary file.

This method is useful when we only want the serialization result of a python object so that we can transfer it over a network or other process.

Syntax of dumps() method

byteobj = pickle.dumps(obj)

Example

import pickle

# convert a string into string of byte and display it on the screen
data = input('Enter a string: ')
byteobj = pickle.dumps(data)
print(byteobj)

Output

Enter a string: Aeroplane
b'\x80\x04\x95\r\x00\x00\x00\x00\x00\x00\x00\x8c\tAeroplane\x94.'

loads() Method

The loads() method reads the string of bytes and reconstructs the replica of the original python object.

Syntax of loads() method

obj = pickle.loads(byteobj)

Example

import pickle

# convert a string of bytes into a python object and display it on the screen
data = b'\x80\x04\x95\r\x00\x00\x00\x00\x00\x00\x00\x8c\tAeroplane\x94.'
print(data)
obj = pickle.loads(data)
print(obj)

Output

b'\x80\x04\x95\r\x00\x00\x00\x00\x00\x00\x00\x8c\tAeroplane\x94.'
Aeroplane

Note: The dump() and dumps() methods are used for serialization or pickling of python objects, whereas the load() and loads() methods are used for deserialization or unpickling of byte streams into python objects.

Database using Binary File

The program given below shows us how we can create and manage a database of records using a binary file.

Example

import pickle
import os

# Create a file if it does not exist
fp = open(r'D:/record.txt','ab+')
fp.close()

while True:
    os.system('cls')
    print('1. Add Record')
    print('2. Display Records')
    print('3. Search Record')
    print('4. Modify Record')
    print('5. Delete Record')
    print('0. Exit')
    choice = int(input('Enter Your Choice : '))

    # Add Record
    if choice == 1:
        with open(r'D:\record.txt', 'ab+') as fileobj:    # open file for adding records

            name = input('Enter Name: ')
            age = int(input('Enter Age: '))
            salary = float(input('Enter Salary :'))
            data = [name, age, salary]      # create a list of data
            pickle.dump(data, fileobj)      # write data to binary file


    # Display Records
    elif choice == 2:
        with open(r'D:\record.txt', 'rb') as fileobj:    # open file for displaying records

            f=0
            print('\nNAME\t\t\tAGE\tSALARY')    # print the heading
            while True:
                try:
                    # load individual record from the file using the load() method
                    data = pickle.load(fileobj)

                    if len(data) !=0:
                        f=1
                        print('%s\t\t\t%d\t%.2f' %(data[0], data[1], data[2]))

                except EOFError:
                    break

            if f==0:
                print('\nRecord does not exist')
            input('\nPress enter to continue...')     # Pause the loop so that the user can see the message


    # Search Record
    elif choice == 3:
        with open(r'D:\record.txt', 'rb') as fileobj:    # open file for searching records

            f=0
            nm = input('Enter name to search: ')
            print('\nNAME\t\t\tAGE\tSALARY')    # print the heading
            while True:
                try:
                    # load individual record from the file using the load() method
                    data = pickle.load(fileobj)

                    if nm == data[0]:
                        f=1
                        print('%s\t\t\t%d\t%.2f' %(data[0], data[1], data[2]))
                        break

                except EOFError:
                    break

            if f==0:
                print('\nRecord does not exist')
            input('\nPress enter to continue...')     # Pause the loop so that the user can see the message


    # Modify Record
    elif choice == 4:

        records = []
        f=0
        nm = input('Enter name to modify record: ')
        with open(r'D:\record.txt', 'rb') as fileobj:    # open file for reading records
            while True:
                try:
                    # load individual record from the file using the load() method
                    data = pickle.load(fileobj)
                    if data[0] == nm:
                        data[0] = input('New Name: ')
                        data[1] = int(input('New Age: '))
                        data[2] = float(input('New Salary: '))
                        f=1

                    # append data to the list (records)
                    records.append(data)

                except EOFError:
                    break

        if f==0:
            print('\nRecord does not exist')
        else:

            # write all records in the file
            with open(r'D:\record.txt', 'wb') as fileobj:    # open file for writing records
                for x in records:
                    pickle.dump(x, fileobj)

        input('\nPress enter to continue...')     # Pause the loop so that the user can see the message


    # Delete Record
    elif choice == 5:

        records = []
        with open(r'D:\record.txt', 'rb') as fileobj:    # open file for reading records
            while True:
                try:
                    # load individual record from the file using the load() method
                    data = pickle.load(fileobj)

                    # append data to the list (records)
                    records.append(data)

                except EOFError:
                    break

        # write all records in the file
        f=0
        nm = input('Enter name to delete record: ')
        with open(r'D:\record.txt', 'wb') as fileobj:    # open file for writing records
            for x in records:
                if x[0] == nm:
                    f=1
                else:
                    pickle.dump(x, fileobj)

        if f==0:
            print('\nRecord does not exist')
        else:
            print('\nRecord deleted successfully')
        input('\nPress enter to continue...')     # Pause the loop so that the user can see the message

    elif choice == 0:
        exit(0)