0% found this document useful (0 votes)
9 views23 pages

Binary File Handling in Python

Notes of Binary files CS class 12th

Uploaded by

neelavyagautam20
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)
9 views23 pages

Binary File Handling in Python

Notes of Binary files CS class 12th

Uploaded by

neelavyagautam20
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

DATA FILE HANDLING (Binary Files)

Binary files
✓ Till now we have written lines/strings in a file.
But there may be situations, when we need to write and read non-simple objects like
dictionary, tuple, list etc. into files.
Some of these objects have a structure and hierarchy associated with their storage.
In order to maintain this structure and hierarchy, we often store these objects in binary files
with the help of a technique called serialization.
✓ They store data in the binary format (0’s and 1’s) .
✓ In Binary files there is no delimiter for a line.
✓ To open files in binary mode, when specifying a mode, add 'b' to it.

Serialization / Pickling
➢ It is the process of converting python object hierarchy into a byte stream so that it can be
written into a file.
➢ Pickling converts an object in byte form in such a way that it can be reconstructed in original
form when unpickled or de-serialised.

De-Serialization / Unpickling
➢ It is the inverse of pickling where a byte stream is converted into an object hierarchy.
➢ Unpickling produces the exact replica of the original object.

Pickle module in Python


➢ Python provides pickle module to read and write objects in a binary file.
➢ Pickle module implements a fundamental but powerful algorithm for serializing and de-
serializing python object structure.
➢ In order to work with pickle module, we must first import it in our program.
➢ To import pickle module, we write the following statement :
Import pickle

STEPS OF OPERATIONS IN BINARY FILES


1. import pickle module.
2. open binary file in the required mode.
3. process binary file by reading/writing objects using pickle module’s methods.
4. close the file.

1|Page
OPENING BINARY FILE
Binary file is opened in the same way as text files. We just need to mention the appropriate file
mode.
Syntax :
file handle = open (name of file , mode)

FUNCTIONS IN PICKLE MODULE


1. dump ( )
it is used to write data in the binary file.
Syntax :
[Link] (object to be written , file handle)

2. load ( )
it is used to read data from binary file.
Syntax :
object = [Link] (file handle)

flush() function
➢ When we write onto a file using any of the write functions, python holds everything to write in
the file in a buffer and pushes it onto actual file on storage device a later time. If however, we
want to force python to write the contents of buffer onto storage, we can use flush( ).
➢ Python automatically flushes the file buffers when closing them, i.e. this function is implicitly
called by the close() function. But you may want to flush the data before closing any file.
➢ The syntax of use flush( ) function is :
[Link]( )

Syntax in Binary Files


1. Opening files
dfile=open(“[Link]”,”wb”)
or
dfile=open(“[Link]”,”rb”)
or
with open(“[Link]”, “wb”) as f:

2. Closing files
[Link]()

2|Page
3. For writing
[Link](object to be written, file_handler)

4. For reading
object=[Link](file_handler)

PROGRAMS ON BINARY FILES


Write data to a Binary File

Read data from a Binary File

WAP to send different types of data in a binary file

3|Page
WAP to read different types of data written in a binary file in the last program

Data is read by load() in the same format as it was sent.

WAP to store roll no and name in a binary file in the form of a list

WAP to store roll no and name in a binary file in the form of a dictionary

4|Page
WAP to store name and age of n people in a binary file in the form of a
dictionary

Method 1 : printing dictionary without formatting

Method 2 : printing dictionary with formatting

5|Page
WAP to copy the contents of one file into another

WAP to send n numbers one by one in the binary file and then reading them.
In binary files, load() reads the data in the same way as it is sent in file using dump().
If we send numbers one by one using dump(), then load() will read them one by one only.

6|Page
Create a binary file with the name ’[Link]’ and stores admission number,
name and percentage of the child record by record.

WAP to read data from the binary file with the name ’[Link]’ created in
last question.

• Since the data has been sent one by one in last program, it will be read one by one using load().
• Also, we do not have a fixed number of entries that have been sent in file in last program, so we
do not have a finite number to run the loop.
• In this case, we need to keep on reading the file till it throws an exception when there is no more
data in the file.
• This makes it mandatory to handle exception using try except block during reading.

Create a binary file with the name ’[Link]’ and stores admission number,
name and percentage of the child. Store all records in one go

7|Page
WAP to read data from the binary file with the name ’[Link]’ created in
last question. (records will be read in one go as they were sent in one go)

Since the data has been sent altogether as a nested list in last program, it will be read as a nested
list using load().
Also, since the whole data will be read at once, we do not need any loop to read the data. Only one
load() statement will read the entire nested list of data.

WAP to read data from the binary file with the name ’[Link]’ created in
last question. (records will be read in one go as they were sent in one go)

Same as last program , but here we will be displaying the records read in the form of a nested list in
a more formatted manner.

WAP to append more records in the file ’[Link]’ created in last question.
(all records sent in one go)

8|Page
Consider the file ’[Link]’ created in last question. WAP to search for a
record in the file whose admission number is given by the user.

9|Page
Consider the file ’[Link]’ created in last question. WAP to count the no.
of records whose percentage is greater than or equal to 90.

Consider the file ’[Link]’ created in last question. WAP to modify the
details of the student whose admission number is entered by the user.

10 | P a g e
Consider the file ’[Link]’ created in last question. WAP to delete the
details of the student whose admission number is entered by the user.

Method 1 : using del

Method 2 : using remove()

11 | P a g e
A binary file “[Link]” has structure [BookNo, Book_Name, Author, Price].
a) Write a user defined function CreateFile() to input data for a record and
add to [Link]
b) Write a function CountRec(Author) in Python which accepts the Author
name as parameter and count and return number of books by the given
Author are stored in the binary file “[Link]

12 | P a g e
A binary file “[Link]” has structure [admission_number, Name,
Percentage].
a) Write a user defined function CreateFile() to input data for a record and
add to [Link]
b) Write a function display() that displays all the records of the file
c) Write a function countrec() in Python that would read contents of the file
“[Link]” and display the details of those students whose
percentage is above 75. Also display number of students scoring above
75%.

13 | P a g e
Assuming the binary file is containing the following elements in the list:
1. Bus Number 2. Bus Starting Point 3. Bus Destination
a) Write a function create() in python to add records in the file.
b) Write a function in python to search and display details, whose
destination is “Cochin” from binary file “[Link]”.

Write a function addrec() in Python to add more new records at the bottom of
a binary file “[Link]”, assuming the binary file is containing the following
structure : [ bus no , source , destination]

14 | P a g e
Structure of product contains the following elements [product code , product
price]
a) Write a function create() to add records in the file.
b) Write a function searchprod(pc) in python to display the record of a
particular product from a file [Link] whose code is passed as an
argument.

15 | P a g e
MENU DRIVEN PROGRAM ON BINARY FILES TO PERFORM THE FOLLOWING
OPERATIONS
➢ INSERTION OF RECORDS
➢ DISPLAY RECORDS
➢ SEARCHING
➢ UPDATION
➢ DELETION

import pickle

def add() :
f=open("[Link]",'ab')
while True:
print("\n")
admno = int(input("Enter admno : "))
name = input("Enter name : ")
clas = int(input("Enter class : "))
per = int(input("Enter percentage : "))
record = [admno , name , clas , per]
[Link](record , f)
ch = input("Add more records? (Y/N) : ")
if ch=="n" or ch=="N":
break
[Link]()

def display():
f = open('[Link]','rb')
print("\n\n *** Student Records ***")
print("Admno \t Name \t Class \t Percentage")
try:
while True:
R = [Link](f)
for i in R:
print(i , end = ' \t')
print()

16 | P a g e
except:
print('')
[Link]()

def search():
f = open('[Link]','rb')
print("\n\n *** Records whose percentage is greater than 75 ***")
print("Admno \t Name \t Class \t Percentage")
try:
while True:
R=[Link](f)

if R[3] >= 75:


for i in R:
print(i , end = '\t')
print()
except:
print('')

def count():
f = open('[Link]','rb')
cnt=0
try:
while True:
R=[Link](f)
for i in R[1]:
if i == 'a':
cnt += 1
except:
print('')
print("\n\n *** Count of a in name field ***")
print("Number of times a appears : ",cnt)

17 | P a g e
def modify():
f = open('[Link]','rb+')
cnt=0
R=[]
[Link](0)
try:
i=0
while True:
x = [Link](f)
[Link](x)
R[i][3] = R[i][3]+3
i = i+1
except:
print('')
[Link](0)
for rec in R:
[Link](rec,f)
[Link]()
print("*** Percentage of Records incremented by 3 ***")
display()

def delete():
f = open('[Link]','rb')
cnt=0
R=[]
try:
while True:
x = [Link](f)
if x[3]>=33 :
[Link](x)
except:
print('')
[Link]()
f = open('[Link]','wb')
for j in R:
[Link](j,f)

18 | P a g e
[Link]()
print("*** Records with percentage<33 Deleted ***")
display()

def menu():
while True:
print("\n\n ** Menu **")
print("1 : Add")
print("2 : Display all")
print("3 : Search")
print("4 : Count")
print("5 : Modify")
print("6 : Delete")
print("0 : Exit")
ch=int(input("Enter choice : "))
if ch==1:
add()
elif ch==2:
display()
elif ch==3 :
search()
elif ch==4 :
count()
elif ch==5 :
modify()
elif ch==6 :
delete()
elif ch==0 :
break

menu()

19 | P a g e
Types of File Access
Sequential access
With this type of file access one must read the data in order, much like with a tape.

Random access (or direct access)


This type of file access lets you jump to any location in the file, then to any other, etc., all in a
reasonable amount of time.

FILE POINTERS
➢ Every file maintains a file pointer which tells the current position in the file where writing
or reading will take place.
➢ A file pointer works like a bookmark in a book.
➢ Each file object has two integer values associated with it :
o get pointer
o put pointer
➢ These values specify the byte number in the file where reading or writing will take place.
➢ By default get pointer is set at the beginning.
➢ By default put pointer is set at the beginning (when you open file in write/append mode)
➢ By default put pointer is set at the end (when you add record in file in append mode)
➢ There are times when you must take control of the file pointers yourself so that you can
read from and write to an arbitrary location in the file.

Functions associated with file pointers :


⚫ The seek() and tell() functions allow you to set and examine the get pointer/put pointer.

Syntax of tell()
➢ Returns the current position of the getpointer or putpointer from beginning of file (Number
of bytes)
➢ Return type is int datatype.
➢ p=[Link]()
fp is the file stream.
➢ Example:
fout=open("[Link]","w")
[Link]("Welcome Python")
print([Link]( ))
[Link]( )

20 | P a g e
Output:
14

Syntax of seek()
➢ These functions are used to move the record pointer from one position to other position.
➢ [Link](+/- n, position)
fp is the file stream.
+ means in forward direction
- means in backward direction

n means number of bytes

Position means 0 for beginning position


1 for current position
2 for end position

seek() function : (with one argument)


➢ With one argument :
[Link](k, 0)
where k is absolute position from the beginning.
➢ The start of the file is byte 0
➢ It will result in moving the pointer as shown-

➢ Example:
fout=open("[Link]","w")
[Link]("Welcome Python")
[Link](5)
print([Link]( ))
[Link]( )

Output:
5

21 | P a g e
File Pointer calls
➢ [Link](0) Go to start
➢ [Link](0, 1) Stay at the current position
➢ [Link](0, 2) Go to the end of file
➢ [Link](m, 0) Move to (m+1)th byte in the file

File Pointer offset calls


➢ [Link](m, 1) Go forward by m bytes from current pos
➢ [Link](-m, 0) Remains at beginning
➢ [Link](-m, 2) Go backward by m bytes from the end
➢ [Link](m, 2) Remains at the end

Functions under os module


import os
1. getcwd() to know the name of the current working directory
str=[Link]()
2. abspath() returns complete path name of data file.
[Link](filename)
3. rename() used to rename a file
[Link](oldfile_name, new file_name)
4. remove() to delete an existing file
[Link](file_name)
5. truncate(n) Resizes the file to n bytes
file_object.truncate(5)

Relative and Absolute Paths:


➢ We all know that the files are kept in directory which are also known as folders.
➢ Every running program has a current directory which is generally a default directory and
python always see the default directory first.
➢ The absolute paths are from the topmost level of the directory structure.
➢ The relative paths are relative to the current working directory denoted as a dot(.) while its
parent directory is denoted with two dots(..).
➢ Operating System module provides many such functions which can be used to work with
files and directories.
➢ getcwd( ) is a very function which can be used to identify the current working directory
>>> import os

22 | P a g e
>>> cwd=[Link]()
>>> print(cwd)
C:\Users\Neha\AppData\Local\Programs\Python\Python36-32

EXAMPLE TO ILLUSTRATE ABSOLUTE FILE PATH

This code creates the file named demo in the path provided, and not in the default python
directory.

23 | P a g e

You might also like