Uploaded by alexhoody51

binary file handling

advertisement
BINARY FILE HANDLING IN PYTHON
LEARNING OBJECTIVES
After going through the chapter, student will be able to:
Introduction to Binary files
Opening Binary file
Using open() method
Using with statement
Closing Binary files
Working with Binary files
Writing into Binary file: Dump method
Reading from Binary file: load method
Searching a Binary file
Modifying data in a Binary file
Deletion from a Binary file
INTRODUCTION TO BINARY FILES
Till now all read and write methods provided by Python supports reading and writing
data in the form of strings to text files. Although we can use text files to write complex
data structures but text files store complex data structure simply as text and to read it,
we need to parse the text and process it back to its original data structure.
To store Python data structure in its original form, binary files are used. In binary files
complex data structure are written in their original form thus at the time of reading we
get the original data back.
Binary files can range from image files like JPEGs or GIFs, video files like MP4, database
files like mdb, sqlite, archive files like zip, rar, executable files like exe audio files like
MP3s or binary document formats like Word or PDF.
Binary files store data in the binary format (0’s and 1’s) which is understandable by the
machine. So when we open the binary file in our machine, it decodes the data and
displays in a human-readable format.
OPENING BINARY FILE
To perform any operation on files we need to first open a file. After opening we perform
the desired operations and then the file needs to be closed.
USING OPEN FUNCTION
Python has a built in function – open() to open a file. The open() function takes two
arguments and returns a file object.
SYNTAX
file_object = open(filename [, mode])
where
filename is the name of the file to be opened. It can be string constant or a variable. If
the file is not present in the same folder / directory in which we are working we need to
give the complete path where the file is present.
mode is an optional parameter and it describes the type of operation we are going to
perform on file like read, write, append etc. Please note that the default mode is reading.
file_object is the name of the object returned by the open function.
File objects contain methods and attributes that can be used to collect information
about the file you have opened and to manipulate it.
Python File Modes
Mode
Description
'r'
Open a file for reading. (default)
'w'
Open a file for writing. Creates a new file if it does not exist or truncates the file if it
exists.
'x'
Open a file for exclusive creation. If the file already exists, the operation fails.
'a'
Open for appending at the end of the file without truncating it. Creates a new file if it
does not exist.
't'
Open in text mode. (default)
'b'
Open in binary mode.
'+'
Open a file for updating (reading and writing)
EXAMPLE 1
Python Code
Explanation
file= open("EXAPMLE","wb")
will open a binary file called EXAMPLE for writing purpose.
file= open("example.dat","rb+")
will open a binary file called example.dat for both reading and writing purpose.
file= open("example.dat", "ab+")
will open a binary file called example.dat for both appending (writing at the end) and
reading purposes.
USING WITH STATEMENT
Apart from using open() function for creation of file, with statement can also be used.
Using with ensures that all the resources allocated to file objects gets deallocated
automatically once we stop using the file.
SYNTAX
with open() as fileobject :
EXAMPLE 2:
with open("example.dat","r+") as file :
file manipulation statements
CLOSING BINARY FILE
After performing desired operations on file, we need to close it. This can be done using
an in-built function-close().
SYNTAX
fileobject. close()
Closing a file frees up the resources that were tied with the file. Before closing a file, any
material which is not written in file, is flushed off i.e. written to file. So, it is a good
practice to close the file once we have finished using it. If we reassign the file object to
some other file, Python will automatically close the file attached to it earlier.
WORKING WITH BINARY FILES
To store data in binary files, Pickle module is used. Pickle module is used to store any
kind of object in file as it allows us to store python objects with their structure. Pickle
module supports two major operations: Pickling and Unpickling.
Pickling is the process whereby a Python object is converted into a byte stream.
Unpickling is the process by which a byte stream is converted back into the desired
object.
The process of pickling and unpickling a Python data object is known as object
serialization.
To use the pickle module, we first need to import it. It provides two important methodsdump and load.
WRITING INTO A BINARY FILE: DUMP method
The dump method of pickle module is used to write objects to binary file.
SYNTAX
pickle.dump(object, file_object)
where
object: is the Python object that is to be written to a binary file
file_object: is the name of the binary file on which write operation is to be performed
EXAMPLE 3
Write a Python script to store a given list in a binary file “file.dat”.
import pickle
list=[5,10,15,20,25]
file=open('file.dat','wb')
pickle.dump(list,file)
file.close()
EXPLANATION
This Python code first creates a list and then write this list to a binary file ‘file.dat’ using
dump method.
EXAMPLE 4
Write a Python script to store a given dictionary entries containing details of students in
a binary file “file.dat”.
import pickle
#Create dictionary
dict1={'Roll No.':1, 'Name':'Rohit', 'Total':450}
dict2={'Roll No.':2, 'Name':'Anvi', 'Total':310}
dict3={'Roll No.':3,'Name': 'Mudit', 'Total':315}
dict4={'Roll No.':4,'Name':'Ravi', 'Total':425}
#Open file in binary writing mode
file=open('file.dat','wb')
#Write dictionary to binary file
pickle.dump(dict1,file)
pickle.dump(dict2,file)
pickle.dump(dict3,file)
pickle.dump(dict4,file)
file.close()
EXPLANATION
This Python code first creates four dictionaries, opens a binary file in write mode and
then write these dictionaries to a binary file ‘file.dat’ using dump method.
APPENDING DATA
To add more data at the end of the file the file is opened in an append mode. If we open
the existing file with records stored in it, in write mode all the previously stored data gets
deleted from it.
EXAMPLE 5
Q Write a Python script to enter data from user and add it to a binary file “file.dat” and
display data stored in it.
import pickle
dict1={}
#Open file in binary append mode
file=open('file.dat','ab')
choice='y'
while choice=='y':
#Input data
rn=int(input("Enter roll number"))
nam=input("Enter Name")
tot=int(input("Enter Total"))
#Store data in dictionary
dict1['Roll No.']=rn
dict1['Name']=nam
dict1['Total']=tot
#write dictionary into file
pickle.dump(dict1,file)
choice=input("Do you want to enter more records")
file.close()
READING FROM A BINARY FILE: load method
The load method of pickle module is used to read the object from the binary file.
SYNTAX
pickle.object = load(file_object)
where
object: is the Python object which is being read from the binary file
file_object: is the name of the binary file from which the read operation is to be
performed
Note: The pickle.load() function raises EOFError(a run time exception error) when the
end of file is reached while reading from the file. This can be handled either by using try
and except block or using with statement.
EXAMPLE 6
Q Write a Python script to read data from a binary file “file.dat” and display data stored
in it. (using try….pickle)
import pickle
#Open file in binary read mode
file=open('file.dat','rb')
try:
#Loop to read the file till the end of file is reached
while True:
#Read the record from the file into dictionary
dict=pickle.load(file)
#Print the data stored in the dictionary
print (dict)
except EOFError: # This block gets executed when End of file is reached
file.close()
OUTPUT
{'Roll No.': 1, 'Name': 'Rohit', 'Total': 450}
{'Roll No.': 2, 'Name': 'Anvi', 'Total': 310}
{'Roll No.': 3, 'Name': 'Mudit', 'Total': 315}
{'Roll No.': 4, 'Name': 'Ravi', 'Total': 425}
{'Roll No.': 5, 'Name': 'Rama', 'Total': 456}
EXAMPLE 7
Q Write a Python script to read data from a binary file “file.dat” and display data stored
in it. (using with statement)
import pickle
file=open('file.dat','rb')
i=1
try:
while True:
dict=pickle.load(file)
print("Record:",i)
print("Roll No:",dict['Roll No.'])
print("Name:",dict['Name'])
print("Total:",dict['Total'])
i=i+1
except EOFError: # This block gets executed when End of file is reached
file.close()
OUTPUT
Record: 1
Roll No: 1
Name: Rohit
Total: 450
Record: 2
Roll No: 2
Name: Anvi
Total: 310
Record: 3
Roll No: 3
Name: Mudit
Total: 315
Record: 4
Roll No: 4
Name: Ravi
Total: 425
Record: 5
Roll No: 5
Name: Rama
Total: 456
SEARCHING FROM A BINARY FILE
To implement searching, we will
open the file in binary read mode
Input the roll number whose data needs to be searched.
Apply loop to read the file record by record from beginning till end.
Read the record of the file in a dictionary
If the roll number read from the file matches the roll number to be searched then print
the record and display “Search successful”. Also assign the value True to variable found.
If the roll number read from the file does not match the roll number to be searched then
the variable found remains false and "Record not present" is displayed.
Close the file
EXAMPLE 8
Q Write a Python script to read data from a binary file “file.dat”, search the given roll no.
and display the data associated with that roll number.
import pickle
dict={}
found=False
file=open('file.dat','rb')
rn=int(input("Enter rollno to be searched"))
ans='y'
try:
#Read the data from file to dictionary dict
while True:
dict=pickle.load(file)
if dict['Roll No.'] ==rn:
print(dict)
print("Search successful")
found=True
except EOFError:
if found==False:
print("Record not present")
file.close()
OUTPUT
Enter rollno to be searched2
{'Roll No.': 2, 'Name': 'Anvi', 'Total': 310}
Search successful
Enter rollno to be searched6
Record not present
MODIFING BINARY FILES
To implement modification, we are opening our file in both reading and writing modes.
Input the roll number whose data needs to be modified. Steps to perform modification
are:
Apply loop to read the file record by record from beginning till end.
Store the position of file pointer of the beginning of the record
Read the record of the file in a dictionary
If the roll number read from the file matches the roll number to be modified then
increase the total of the desired rollno by 10
Place the file pointer back to the beginning of the record already read so that we can
overwrite the earlier record with new record
Write the modified record back to the file
Break from the loop as the record has been modified
Close the file
After the modification process place the file pointer to the beginning of the file using
seek(o) method. Then use the loop to display the contents of the modified file.
EXAMPLE 9
Q Write a Python script to read data from a binary file “file.dat”, search the given roll no.
and increase the total by 10 for that particular roll number.
import pickle
dict={}
found=False
#Open the file in binary read and write mode
file=open('file.dat','rb+')
#Input the rollno to be modified
rn=int(input("Enter rollno to be searched"))
try:
while True:
#Stores the position of file pointer of the beginning of the record
pos=file.tell()
#Read the record in the dictionary dict
dict=pickle.load(file)
#check whether the rollno fetched is equal to the rollno to be modified
if dict['Roll No.']==rn:
# Increase the total of the desired rollno by 10
dict['Total']+=10
#Place the file pointer back to the beginning of the record already read
file.seek(pos)
#Write the modified record back to the file
pickle.dump(dict,file)
found=True
# Break from the loop as the record has been modified
break
except EOFError:
# This block gets executed when End of file is reached
if found==False:
print("Record not present")
else:
print("Modification successful")
# Place the file pointer at the beginning of the file
file.seek(0)
try:
while True: #Run the loop till the end of file is reached
# Read record from the file into the dictionary
dict=pickle.load(file)
#Display the record which has been read
print(dict)
except EOFError:
# This block gets executed when End of file is reached
file.close()
OUTPUT
Enter rollno to be searched4
{'Roll No.': 1, 'Name': 'Rohit', 'Total': 470}
{'Roll No.': 2, 'Name': 'Anvi', 'Total': 310}
{'Roll No.': 3, 'Name': 'Mudit', 'Total': 315}
{'Roll No.': 4, 'Name': 'Ravi', 'Total': 435}
{'Roll No.': 5, 'Name': 'Rama', 'Total': 456}
Here, total of roll number has been increased by 10.
DELETION FROM BINARY FILES
To implement deletion, we are reading from one file and transferring all the data in
another file except the record which is to be deleted. Steps to perform deletion are:
Open the file from which the record is to be deleted in read mode.
Open one more file in write mode. Input the roll number to be deleted.
Read the first file record by record from beginning till end.
If the roll number read from the file does not match with the roll number to be deleted
then write the read record into the new file.
If the roll number read from the file matches the roll number to be deleted then do not
write the read record into the new file.
Close both files
After the deletion process place the file pointer to the beginning of the file using seek(o)
method. Then use the loop to display the content of newly created file.
EXAMPLE 10
Q Write a Python script to read data from a binary file “file.dat”, search the given roll no.
and delete that particular roll number.
import pickle
dict={}
#Open the file from which the record is to be deleted in binary read mode
file=open('file.dat','rb')
#Open one more file in binary read and write mode to transfer
#all records except the record to be deleted
Newfile=open('file1.dat','wb+')
#Input the rollno to be deleted
rn=int(input("Enter rollno to be deleted"))
try:
while True: #Run the loop till the end of file is reached
# Read record from the file into the dictionary
dict=pickle.load(file)
if dict['Roll No.']!=rn:
#If the roll number read from the file does not match the roll number to be
deleted
#then write the record into new file
pickle.dump(dict,Newfile)
except EOFError:
file.close()
#Place the file pinter to the beginning of the file
Newfile.seek(0)
try:
while True: #Run the loop till the end of file is reached
# Read record from the file into the dictionary
dict=pickle.load(Newfile)
print(dict)
except EOFError: # This block gets executed when End of file is reached
Newfile.close()
OUTPUT
Enter rollno to be deleted3
{'Roll No.': 1, 'Name': 'Rohit', 'Total': 470}
{'Roll No.': 2, 'Name': 'Anvi', 'Total': 310}
{'Roll No.': 4, 'Name': 'Ravi', 'Total': 435}
{'Roll No.': 5, 'Name': 'Rama', 'Total': 456}
Here, record with roll numner 3 has been deleted.
Download