0% found this document useful (0 votes)
17 views

PYTHON-Programs_Binary-File-Handling

The document provides comprehensive instructions on handling binary files in Python, including reading, writing, and modifying various data structures such as lists, tuples, sets, matrices, and dictionaries. It also covers serialization and deserialization using the pickle module, detailing methods like dump(), load(), dumps(), and loads(). Examples illustrate how to implement these operations effectively.

Uploaded by

abhiyuth08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

PYTHON-Programs_Binary-File-Handling

The document provides comprehensive instructions on handling binary files in Python, including reading, writing, and modifying various data structures such as lists, tuples, sets, matrices, and dictionaries. It also covers serialization and deserialization using the pickle module, detailing methods like dump(), load(), dumps(), and loads(). Examples illustrate how to implement these operations effectively.

Uploaded by

abhiyuth08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

PYTHON Programs: File Handling: Binary files

1. The concept of binary files. Presentation of information in binary files


# Python. Work with binary files
# Open binary file for reading
f = open('myfile1.bin', 'rb')
# Get a string from binary file
d = f.read()

# Display this string. The output will be as a string of characters


print("d = ", d) # d = b'\x80\x03]q\x00(K\x01\x88G@\x07\n=p\xa3\xd7\ne.'

# If print as a separate character, then the character code will be displayed - as an integer
print("d[5] = ", d[5]) # d[5] = 40
print("d[0] = ", d[0]) # d[0] = 128

# Use bin function for single character


print(bin(d[2])) # 0b1011101
f.close()
output
d = b'\x80\x03]q\x00(K\x01\x88G@\x07\n=p\xa3\xd7\ne.'
d[5] = 40
d[0] = 128
0b1011101

2. Writing / reading a list that contains real numbers. Example


# Binary files Writing / reading a list of real numbers

# 1. Given list of real numbers


L = [1.5, 2.8, 3.3]

# 2. File writing
# 2.1. Open file for writing
f = open('myfile3.bin', 'wb')

# 2.2. Bypass list and write data to a file


for item in L:
# add the character '\ n' so that numbers can be recognized
s = str(item) + '\n'
# Encode () method - converts a string to a sequence of bytes
bt = s.encode()
f.write(bt) # write() method - write to file

# 2.3. Close file


f.close();

# 3. Read list from binary file 'myfile3.bin'


# 3.1. Create an empty list
L2 = []

# 3.2. Open file for reading


f = open('myfile3.bin', 'rb')

# 3.3. Bypass lines of a file, converting and adding to the list L2


for ln in f:
x = float(ln) # get a number
L2 = L2 + [x] # Add number to list

# 3.4. Output the list


print("L2 = ", L2) # L2 = [1.5, 2.8, 3.3]

# 3.5. Close file


f.close();
Output
L2 = [1.5, 2.8, 3.3]

3. Writing/reading a tuple containing character strings. Example


# Binary files. Writing / reading a tuple containing character strings

# 1. The specified tuple with strings


T = ( 'abc', 'def', 'ghi', 'jk lmn')

# 2. Write tuple T to file 'myfile5.bin'


# 2.1. Open file for writing
f = open('myfile5.bin', 'wb')

# 2.2. Tuple bypass cycle


for item in T:
bt = (item + '\n').encode() # convert (str + '\n') => bytes
f.write(bt) # write bt to file
# 2.3. Close file
f.close();

# 3. Read tuple from binary file 'myfile5.bin'


# 3.1. Open file for reading
f = open('myfile5.bin', 'rb')

# 3.2. A new tuple


T2 = ()

# 3.3. Read data from file


for line in f:
s = line.decode() # convert bytes=>str
s = s[:-1] # Remove the last character
T2 = T2 + (s,) # Add string s to tuple

# 3.4. Print the tuple


print("T2 = ", T2)

# 3.5. Close file


f.close();
Output
T2 = ('abc', 'def', 'ghi', 'jk lmn')

4. Writing / reading a set containing real numbers. Example


# Binary files. Writing/reading a set that contains real numbers

# 1. Given set
M = { 0.2, 0.3, 0.8, 1.2, 1.77 }

# 2. Writing the set M to the file 'myfile6.bin'


# 2.1. Open file for writing
f = open('myfile6.bin', 'wb')

# 2.2. The loop of bypass the set


for item in M:
s = str(item) + '\n' # convert float=>str + '\n'
bt = s.encode() # convert str=>bytes
f.write(bt) # write bt to file
# 2.3. Close file
f.close();

# 3. Read set from binary file 'myfile6.bin'


# 3.1. Open file for reading
f = open('myfile6.bin', 'rb')

# 3.2. Create a new set


M2 = set()

# 3.3. Read data from file


for line in f:
x = float(line) # convert bytes=>x
M2 = M2.union({x}) # add x to the set

# 3.4. Print the set


print("M2 = ", M2)

# 3.5. Close file


f.close()
Output
M2 = {0.2, 0.8, 0.3, 1.77, 1.2}

5. Writing/reading a two-dimensional matrix of rows of a given size. Example


# Binary files. Writing/reading a matrix that contains rows

# 1. Given matrix of rows 3*4 in size

MATRIX = [ [ 'aa', 'ab', 'ac', 'ad'],


[ 'ba', 'bb', 'bc', 'bd'],
[ 'ca', 'cb', 'cc', 'cd'] ]

# 2. Writing MATRIX matrix to the file 'myfile7.bin'


# 2.1. Open file for writing
f = open('myfile7.bin', 'wb')

# 2.2. First, write the size of the matrix


m=3
n=4

# convert m, n to str type


sm = str(m) + '\n'
sn = str(n) + '\n'

# convert str to bytes


bm = sm.encode()
bn = sn.encode()

# write matrix sizes to file


f.write(bm)
f.write(bn)

# 2.3. The loop of matrix traversal


for row in MATRIX:
# here you just need to write lines with the character '\n'
for item in row:
item = item + '\n'
bt = item.encode() # str=>bytes
f.write(bt) # write bt to file

# 2.3. Close file


f.close();

# 3. Read matrix from binary file 'myfile7.bin'


# 3.1. Open file for reading
f = open('myfile7.bin', 'rb')

# 3.2. A new matrix


MATRIX2 = []

# 3.3. Read data from file


# 3.3.1. First, read size
s = f.readline()
m2 = int(s)
s = f.readline()
n2 = int(s)

# 3.3.2. Loop of reading the lines and the creation of a matrix size m2*n2
i=0
while i < m2: # m2 rows in matrix
row = [] # one row in list
j=0
while j < n2:
bs = f.readline() # read one element of type bytes
s = bs.decode() # convert bytes=>str
s = s[:-1] # remove '\n'
row += [s] # add to the list
j = j+1
MATRIX2 += [row] # add one row of the list to the matrix
i = i+1

# 3.4. Display new matrix


i=0
while i < m2:
print("MATRIX2[", i, "] = ", MATRIX2[i])
i = i+1

# 3.5. Close file


f.close()
Output
MATRIX2[ 0 ] = ['aa', 'ab', 'ac', 'ad']
MATRIX2[ 1 ] = ['ba', 'bb', 'bc', 'bd']
MATRIX2[ 2 ] = ['ca', 'cb', 'cc', 'cd']

6. Writing/reading a dictionary. Example


# Binary files. Writing/reading a dictionary

# 1. The specified dictionary. Pairs of type str:int


D = { 'One':1, 'Two':2, 'Three':3, 'Four':4 }

# 2. Writing D dictionary to file 'myfile8.bin'


# 2.1. Open file for writing
f = open('myfile8.bin', 'wb')

for key in D: # dictionary bypass


# get the value
value = D[key]

# Write sequentially key, then value


svalue = str(value) + '\n' # convert value to string
skey = key + '\n' # add '\n' to key string

# Convert key:svalue from string to bytes


b_key = skey.encode()
b_svalue = svalue.encode()

# write b_key, b_svalue to the file


f.write(b_key)
f.write(b_svalue)

# 2.3. Close file


f.close();

# 3. Read dictionary from binary file 'myfile8.bin'


# 3.1. Open file for reading
f = open('myfile8.bin', 'rb')

# 3.2. New dictionary to be read from file


D2 = dict()

# 3.3. Read the whole file at once


b_strings = f.readlines() # b_strings - list of strings of type bytes

# 3.4. Bypass the b_strings list.


# The key is read first, then the value, etc.
fkey = True # if True, then the key is read, otherwise the value is read
for item in b_strings:
if fkey: # check if key is read
skey = item.decode() # convert bytes=>str
key = skey[:-1] # remove '\n'
fkey = False
else:
svalue = item.decode() # convert bytes=>str
value = int(svalue) # convert str=>int
D2[key] = value # add to dictionary
fkey = True # indicate that the next iteration will be the key

# 3.5. Output the dictionary


print("D2 = ", D2)

# 3.6. Close file


f.close()
Output
D2 = {'One': 1, 'Two': 2, 'Three': 3, 'Four': 4}
7. Copying one binary file to another
# Binary files. Copying files

# 1. Open the files


f1 = open('myfile8.bin', 'rb') # file - source, it is opened for reading
f2 = open('copyfile8.bin', 'wb') # file - copy

# 2. Read file f1 to bstrings string list


bstrings = f1.readlines()

# 3. Write bstrings string list to f2 file


f2.writelines(bstrings)

# 4. Close the files


f1.close()
f2.close()

8. Combining two binary files. Example


# Combining files myfile1.bin+myfile2.bin => myfile3.bin
# 1. Open files to read
f1 = open('myfile1.bin', 'rb')
f2 = open('myfile2.bin', 'rb')

# 2. Read files into L1, L2 lists


L1 = f1.readlines()
L2 = f2.readlines()

# 3. Combine lists
L3 = L1 + L2

# 4. Close files myfile1.bin, myfile2.bin


f1.close()
f2.close()

# 5. Open file myfile3.bin to write


f3 = open('myfile3.bin', 'wb')

# 6. Write list L3 into the file


f3.writelines(L3)
# 7. Close the result file
f3.close()
9. Python program to modify the content of a Binary File
# Python program to modify the content of binary file
def update_binary(word, new):
# string variable to store each word after reading from the file
string = b""
# Flag variable to check if the record is found or not
Flag = 0
# Open the file in r + b mode which means opening a binary file for reading and writing
with open('file.txt', 'r + b') as file:
pos = 0
# Reading the content of the file character by character
data = string = file.read(1)
# Looping till the end of file is reached
while data:
data = file.read(1)
# Checking if the space is reached
if data == b" ":
# checking the word read with the word entered by user
if string == word:
# Moving the file pointer at the end of the previously read record
file.seek(pos)
# Updating the content of the file
file.write(new)
Flag = 1
break
else:
# storing the position of current file pointer i.e. at the end of previously read record
pos = file.tell()
data = string = file.read(1)
else:
# Storing the data of the file in the string variable
string += data
continue
if Flag:
print("Record successfully updated")
else:
print("Record not found")
# Driver code Input the word to be found and the new word
word = input("Enter the word to be replaced: ").encode()
new = input("\nEnter the new word: ").encode()
update_binary(word, new)
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 Un-pickling
Deserialization or Un-pickling 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)

You might also like